我的代码似乎有问题,代码可以运行但不是预期的。我们的想法是程序读取一个名为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
答案 0 :(得分:2)
由于您没有正确获取文件名,因此未发布任何内容。
更改
scanf("%s", &filename); /* %s expects a pointer, filename is already a pointer */
到
scanf("%s", filename);
答案 1 :(得分:1)
int main()
{
char filename[32] = { '\0' } ;
...
scanf("%s", &filename);
在scanf()
中,&filename
不正确,因为filename
是32个元素的char数组的基址。 fopen()
地址filename
后scanf("%s", filename);
也不正确。而应该读入指向文件名为const char s[4] = ":";
s
此处的分隔符:
不必是4个字符的数组,因为您只想在每个const char *s = ":";
分隔。如果你有{{1}}
答案 2 :(得分:1)
主要问题似乎是strncpy(TempStorage, TempData, 50);
,因为TempStorage是用[5]声明的,而strncpy使用50覆盖了TempStorage,破坏了相邻的变量。
这使用sscanf从字符串中提取值。
#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".
if ( ErrorFile == NULL) {
printf ( "Could not open error file\n");
return 2;
}
printf("File has been found, checking validation\n");
while( fgets (DataLine, 75, DataFile)!=NULL) {
strcpy(ErrorLine, DataLine);
sscanf ( DataLine, "%d:%d:%d:%d:%49[^\n]"
, &TempS
, &TempD
, &TempT
, &TempP
, TempData);
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");
if ( DFile == NULL) {
printf ( "Could not open data file\n");
return 1;
}
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;
}