我这里有一个代码,需要验证txt文件和过滤数据。我的代码需要使用struct和realloc。
这里的想法是首先读取txt文件并将其存储在数组中。然后,它将根据下面的条件过滤出数据。符合条件的将转到data_1.txt,其他将转到error.txt。
下面是结构的错误,对此我不确定。
error C2440: '=' : cannot convert from 'void *' to 'Validation *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
error C2062: type 'void' unexpected
error C2065: 'src' : undeclared identifier
error C2065: 'dest' : undeclared identifier
error C2065: 'type' : undeclared identifier
error C2065: 'port' : undeclared identifier
示例源文件:
9001:0002:9003:0021
0001:0010:0003:0021
8001:0002:8002:0080
源代码:
#include <stdio.h>
#include <stdlib.h> //for exit (1)
#include <malloc.h>
struct Validation {
int src;
int dest;
int type;
int port;
char data[50];
};
int main ()
{
struct Validation *array;
int option;
FILE *inFile; //file handle or pointer
char filename[100];
char test;
int count = 0; //keep track number of records
int n1 = 0; //keep track number of correct records
int n2 = 0; //keep track number of error records
array = (struct Validation *) malloc (sizeof (struct Validation));
do {
printf ("Enter filename: ");
scanf ("%s", filename);
printf ("Processing filename %s ...\n", filename);
inFile = fopen (filename, "r");
if (inFile == NULL) //check if file handler is invalid
{
printf ("Could not open file %s\n", filename);
exit (1); //error status code
}
test = fscanf (inFile, "%d:%d:%d:%d:",
&array[count].src, array[count].dest,
array[count].type, array[count].port);
while (test != EOF) {
count++;
array = realloc (array, (count + 1) * sizeof (struct Validation));
test = fscanf (inFile, "%d:%d:%d:%d:",
&array[count].src, array[count].dest,
array[count].type, array[count].port);
}
}
void save (int, struct Validation *);
FILE *outFile;
FILE *errorFile;
int i;
outFile = fopen ("data_1.txt", "wt");
errorFile = fopen ("data_error.txt", "wt");
if (outFile == NULL) //check if file handler is invalid
{
printf ("Could not write to file \n", filename);
exit (1);
}
if (count > 0) {
printf ("Viewing all records: ", count);
for (i = 0; i < count;
i++, src >= 1 && src <= 1024 && dest >= 1 && dest <= 1024
&& type >= 1 && type <= 10 && port >= 1 && port <= 1024) {
fprintf (outFile, "%d %d %d %d",
(i + 1),
array[i].src,
array[i].dest,
array[i].type,
array[i].port);
n1++;
}
} else {
fprintf (errorFile, "%d %d %d %d",
array[i].src, array[i].dest, array[i].type, array[i].port);
n2++;
}
fclose (errorFile);
fclose (outFile);
fclose (inFile); //must always close file once done
free (array);
return 0;
}
最佳答案
有了这个程序中的所有错误,我只能得出这样的结论:你用来学习C语言的资源不起作用;它教你各种各样的错误,既有阻止你的程序编译的错误,也有阻止你的程序正常运行的错误。我建议你在继续学习本课程之前,先阅读K&R第二版,并做练习。
首先,您的项目包含一些严重的语法错误,当您修复缩进时会发现这些错误。如果你不知道怎么做,考虑学习this wiki page on indent styles。
你的后四个错误在这一行:
for (i = 0; i < count; i++, src >= 1 && src <= 1024 && dest >= 1 && dest <= 1024 && type >= 1 && type <= 10 && port >= 1 && port <= 1024)
^----------
首先,您的错误告诉您
src
、dest
、type
和port
是未声明的。也许你想写的是array[i].src
,array[i].dest
,array[i].type
和array[i].port
。。。我指的逗号可能不是你想的那样。即使您修复了这一行中的错误,逗号之后的所有内容都对代码没有任何影响,因此您的循环是有效的:
for (i = 0; i < count; i++)
我只能冒险猜测一下,但也许
for
(pre-test)循环不适合您的需要,您应该改用if (part_of_test) do { ... } while (test);
习惯用法?也许这太过分了,您可以只使用一个do { ... } while (test);
白痴来编写所需的功能。while (test != EOF)
这个循环也是错误的。如果要确保从
inFile
中正确读取四个十进制数字整数,则需要确保test
等于4
,这与确保test
等于EOF
不同。例如:while (test == 4)
继续前进,你似乎正在试图编译你的C代码作为C++。
虽然某些C代码在C++中也是有效的,但是您遇到了C++中无效的一个C代码的例子。
也就是说,C++不提供对C/CC的隐式转换。
您应该始终使用C编译器编译C代码,原因如下:
你不需要用C++编译器编译C代码。
你错过了C++最好的部分。
你错过了C最好的部分。
人们试图用C++编译器编译C代码的唯一原因是他们希望在C++项目中使用C代码。但是,这是不必要的,因为您通常可以使用C编译器编译C代码,使用C++编译器编译C++代码,然后将这两个代码链接在一起。例如:
gcc -c c_code.c
g++ -c cpp_code.cpp
gcc cpp_library.o c_library.o
如果使用的是Microsoft Visual Studio,请将C源代码分离到不同的项目中,然后浏览项目设置以查找“编译为C代码”选项。如果你的项目是C语言的,你可以忽略这一段的其余部分。。。否则,你的项目是混合的C和C++。在浏览时,请注意“链接”和“项目依赖”设置;您希望将C项目设置为C++项目的依赖项。您可能还想介绍一些“链接目录”(或者路径,我忘记了确切的术语)和/或“包含目录”。
在C++中,使用
void *
指针(通常应该是AA>)通常是个坏主意。更不用说,您应该避免使用void *
函数族。那些专门在C++中编程的人通常会畏缩于这些实践,因为模板和运算符重载实际上消除了所有有效的用例,从而使相应的C++代码不易出错,并且直接向前读写。在C语言中,显式地强制转换
new
(您应该消除当前使用的malloc
强制转换)总是一个坏主意,正如probably use *scanf
instead of void *
所解释的。那些专门用C语言编程的人会对可能引入错误的不必要的样板文件感到畏缩不前。关于c - 使用struct验证文件并重新分配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33796363/