C 错误函数未正确执行

C 错误函数未正确执行,c,C,我的代码似乎有问题,代码可以正常工作,但没有达到预期效果。其思想是程序读取一个名为data.txt的数据文件,读取每一行,该行有自己的元素,元素之间用:标记分隔 代码读取此信息,将数据存储在变量中,然后将其与我的验证规范进行比较,然后程序会将所有通过验证的正确行发布到data.txt文件中,但error.txt文件中没有发布任何内容 #include <stdio.h> //library including standard input and output fu

我的代码似乎有问题,代码可以正常工作,但没有达到预期效果。其思想是程序读取一个名为
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
{
scanf()
中,
&filename
不正确,因为
filename
是32个元素的字符数组的基址。在
filename
的地址上的
fopen()
后面也不正确。相反,您应该读入指向文件名的数组的基址为
scanf(“%s”,文件名)

const char s[4]=“:”
此处的分隔符
s
不需要是4个字符的数组,因为您仅尝试在每个
处进行分隔:
。因此,如果您有
const char*s=“:”

scanf()
中,
&filename
不正确,因为
filename
是32个元素的字符数组的基址。在
filename
的地址上的
fopen()
后面也不正确。相反,您应该读入指向文件名的数组的基址为
scanf(“%s”,文件名)


const char s[4]=“:”
此处的分隔符
s
不需要是4个字符的数组,因为您仅尝试在每个
处进行分隔:
。因此,如果您有
const char*s=“:”

由于没有正确获取文件名,因此未发布任何内容

改变

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


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

改变

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


主要问题似乎是
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;
}
#包含//库,包括标准输入和输出函数
#包含//库,包括下面使用的退出和系统功能
#包含//库,包括使用的字符串函数
结构包{
int source;//1-1024范围(int)
int destination;//1-1024范围(int)
int-type;//0-10范围(int)//结构的变量
int端口;//1=1024(int)
字符数据[50];//1-50范围(字符)
};
int main()
{
char filename[32]={'\0'};//声明I/O流和文件名结构的变量
char数据行[75];//一次读取一行文件
char ErrorLine[75];//这是处理验证错误的变量
char TempStorage[5];//存储要验证的数据
char TempData[50];//存储将要验证的数据
int TempS,TempD,test,TempP;//存储从输入文件派生的整数
int Flag=0;//这是指示行未通过验证的标志
int Count=0;//这是指示行已通过验证的标志
int Ecount=0;//这计算错误数
char*ptr;
字符*令牌;
常量字符s[4]=“:”;
结构包*数据记录;
DataRecords=malloc(sizeof(struct packet));//用于存储下一个任务所需的数据。
//程序必须提示输入文件名。如果输入文件不存在,程序应停止并显示错误消息
printf(“输入要打开的文件名\n”);
scanf(“%s”,文件名);
//用户输入文件名
文件*数据文件;
if((数据文件=fopen(文件名,“r”))==NULL)
{
printf(“\n无法打开文件。:%s\n”,文件名);//如果返回NULL值,则程序将关闭。
}
其他的
{
FILE*ErrorFile=fopen(“error.txt”,“w”);//这将开始搜索各行,并将未通过验证测试的行存储到名为“error.txt”的txt文件中。
if(ErrorFile==NULL){
printf(“无法打开错误文件\n”);
返回2;
}
printf(“已找到文件,正在检查验证\n”);
while(fgets(数据线,75,数据文件)!=NULL){
strcpy(错误行、数据行);
sscanf(数据线,“%d:%d:%d:%d:%49[^\n]
和临时工
和临时工
,&诱惑
,&TempP
,TempData);
如果(TempS<1 | | TempS>1024){
Flag=1;
}
如果(TempD<1 | | TempD>1024){
Flag=1;
}
如果(试探<0 | |试探>10){
Flag=1;///验证方面,如果未满足验证要求,则会添加一个标志,然后在错误文件中发布该行。
}
如果(TempP<1 | | TempP>1024){
Flag=1;
}
如果(strlen(TempData)<1 | | strlen(TempData)>50){
Flag=1;
}
如果(标志==1)
{
printf(“错误%i%i:%i:%i:%i:%s”,Ecount、TempS、TempD、test、TempP、TempData);
ecoount++;
fprintf(错误文件,“%s”,错误行);
//fprintf将格式化文本写入指定的输出流
}
其他的
{
数据记录[计数]。源=临时;
数据记录[Count]。目的地=TempD;
DataRecords[Count].type=诱惑;
数据记录[计数]。端口=TempP;
strncpy(DataRecords[Count].data,TempData,51);
scanf("%s", &filename); /* %s expects a pointer, filename is already a pointer */
scanf("%s", filename);
#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;
}