gpt4 book ai didi

c - 错误函数未正确执行

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:36 25 4
gpt4 key购买 nike

我的代码似乎有问题,代码可以工作,但不符合预期。这个想法是程序读取一个名为 data.txt 的数据文件,读取每一行并且该行有自己的元素,由 : 标记分隔。

代码读取它,将数据存储在一个变量中,然后将其与我的验证规范进行比较,然后程序将所有已通过验证的正确行发布到 data.txt 文件,但 error.txt 文件中没有任何内容。

#include <stdio.h>          //library including standard input and output functions
#include <stdlib.h> //library including exit and system functions used below
#include <string.h> //library including string functions used

struct packet{
int source; // 1 - 1024 range (int)
int destination; // 1 - 1024 range (int)
int type; // 0 - 10 range (int) // Varibles for the structure
int port; // 1 = 1024 (int)
char data[50]; // 1 - 50 range (char)

};

int main()
{

char filename[32] = { '\0' } ; // variables which declare the I/O stream and the filename structure
char DataLine[75]; // Reads the file one line at a time
char ErrorLine[75]; // This is the varible that deals with the validation error
char TempStorage[5]; // Stores data to be validated
char TempData[50]; // Stores the data which will be validated
int TempS, TempD, TempT, TempP; // Stores the integer derived from the input file
int Flag = 0; // This is the Flag that indicates a Line has not passed validation
int Count = 0; // This is the Flag that indicated a line has passed validation
int Ecount = 0; // This counts the number of errors
char *ptr;
char *token;
const char s[4] = ":";

struct packet *DataRecords;
DataRecords = malloc(sizeof(struct packet)); // This deals with storing the data needed for the next task.

// The program must prompt for the name of the input file. If it doesn't exist the program should stop with an error message

printf("Enter the filename you wish to open\n");
scanf("%s", &filename);
// user inputs the filename
FILE *DataFile;
if (( DataFile = fopen(filename, "r")) == NULL)
{
printf ("\nfile could not be opened. : %s\n", filename); // If a value of NULL is returned then the program will close.

}
else
{

可能发生错误的地方:

FILE *ErrorFile = fopen("error.txt","w");                         // This will start searching through the lines and store the lines not passing the validation test to a txt file named "error.txt".
printf("File has been found, checking validation");

while( fgets (DataLine, 75, DataFile)!=NULL) {
strcpy(ErrorLine, DataLine);

strcpy(TempStorage, token = strtok(DataLine, s));
TempS = strtol (TempStorage, NULL, 10);

strcpy(TempStorage, token = strtok(NULL, s)); // these lines of code looks through each line and stores the line within the "Temp Storage" variable, the : token is what the element within the line is seperated by.
TempD = strtol (TempStorage, NULL, 10);

strcpy(TempStorage, token = strtok(NULL, s));
TempT = strtol (TempStorage, NULL, 10);

strcpy(TempStorage, token = strtok(NULL, s));
TempP = strtol (TempStorage, NULL, 10);

strcpy(TempData, strtok( NULL, ":"));
strncpy(TempStorage, TempData, 50); // security details

if (TempS < 1 || TempS > 1024) Flag = 1;
if (TempD < 1 || TempD > 1024) Flag = 1;
if (TempT < 0 || TempT > 10) Flag = 1; // // Validation aspect, if the validation is not met then a flag is added to which then the line is posted within the error file.
if (TempP < 1 || TempP > 1024) Flag = 1;
if (strlen(TempData) < 1 || strlen(TempData)> 50) Flag = 1;

if (Flag == 1)
{
printf("Error %i %i:%i:%i:%i:%s",Ecount,TempS,TempD,TempT,TempP,TempData);
Ecount++;
fprintf(ErrorFile,"%s", ErrorLine);
// fprintf writes formatted text to the output stream you specify
}
else
{
DataRecords[Count].source = TempS;
DataRecords[Count].destination = TempD;
DataRecords[Count].type = TempT;
DataRecords[Count].port = TempP;
strncpy(DataRecords[Count].data,TempData,51);
Count++; //increment sequence number
DataRecords = realloc(DataRecords,(Count+1)*sizeof(struct packet));//allocate more memory for packet data
}
Flag = 0;
}
FILE *DFile = fopen("data.txt","w");
int i;
for (i = 0; i < Count; i++)
{
fprintf(DFile, "%04i:%04i:%04i:%04i:%s",DataRecords[i].source, // Where the data that has passed validation goes
DataRecords[i].destination,
DataRecords[i].type,
DataRecords[i].port,
DataRecords[i].data);
}
fclose(DFile);
fclose(DataFile);
fclose(ErrorFile);
printf("\nNumber of errors: %i \n", Ecount);
printf("Number of saved records: %i ", Count);
free(DataRecords);

}
return 0;
}

这是 data.txt 文件中的数据:

1025:2222:1231:1312:0000
0002:0004:0002:0090:100000000000000000022
0001:0002:0003:0021:DEL
0002:0004:0002:0010:100000000000000000023
0001:0002:0002:0080:PAGE 1<BR>
0003:0004:0002:0180:100000000000000000026
0004:0004:0002:0180:100000000000000000027

最佳答案

没有发布任何内容,因为您没有正确获取文件名。

改变

scanf("%s", &filename); /* %s expects a pointer, filename is already a pointer */

scanf("%s", filename);

关于c - 错误函数未正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26098388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com