Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文本文件中删除数据(c编程)_C - Fatal编程技术网

从文本文件中删除数据(c编程)

从文本文件中删除数据(c编程),c,C,我正在用c语言编程 用于添加学生信息,例如(姓名、TP、国籍、性别、年龄) 并且更喜欢一些功能,例如删除 对于删除部分,我将信息移动到另一个文件中,但我选择的学生除外,或者我键入了他的TP号码来删除。 但是阅读部分有问题,所以它不会删除 void add () e = fopen_s(&f,"student.txt","a+"); int studentcount ; printf("pelase enter how many students u want to add"); sc

我正在用c语言编程 用于添加学生信息,例如(姓名、TP、国籍、性别、年龄) 并且更喜欢一些功能,例如删除 对于删除部分,我将信息移动到另一个文件中,但我选择的学生除外,或者我键入了他的TP号码来删除。 但是阅读部分有问题,所以它不会删除

void add ()


e = fopen_s(&f,"student.txt","a+");
int studentcount ;
printf("pelase enter how many students u want to add");
scanf("%d",&studentcount);
if(e!=0)
{
    printf("File could not be created.");
    exit(1);
}
for(i = 0; i<studentcount;i++)
{
    fflush(stdin);
    printf("\nPlease enter the student name : ");
    gets(name);
    printf("\nPlease enter the student nationality : ");
    gets(nationality);
    printf("\nPlease enter the student gender : ");
    gets(gender);
    printf("\nPlease enter the student age : ");
    scanf("%d", &age);
    printf("\nPlease enter the TP number : ");
    scanf("%d", &TP);
    printf("\nPlease enter the student contact number : ");
    scanf("%d", &contact);
    fprintf(f,"%i\t %s\t %s\t %s\t %d\t ;%d;\t %d\n",strlen(name), name, nationality, gender, age, TP,contact);
}


fclose(f);

main();

以相同格式读取的文件中打印的格式。这样做就容易了

add()
函数
fprintf
中进行以下更改。因此,当使用
fscanf
delete
中读取时,将很容易

fprintf(f,"%i\t %s\t %s\t %s\t %d\t %d\t %d\n",strlen(name), name, nationality, gender, age, TP,contact); // don't use semi colon in between
而不是这种逻辑-

c = getc (originalfile);
while (c !=EOF)
{
     c = getc (originalfile);
     if (c == '\n')
     temp++;
     if (c != deltp)
     {
        fputc (c,newfile);
     }
}
用这个逻辑——

int len;
printf ("please enter the student TP\n");
scanf ("%d",&deltp);
FILE *newfile =  fopen ("student_temp.txt","w");
while((fscanf(originalfile,"%i %s %s %s %d %d %d",&len, name, nationality, &gender, age, &TP, &contact))!=EOF){
    if(deltp != TP)
    fprintf(newfile,"%i\t %s\t %s\t %s\t %d\t ;%d;\t %d\n",strlen(name), name, nationality, gender, age, TP,contact);
}
fclose(newfile);
fclose(originalfile);

希望这对你有帮助

您可以自己调试这个:
int len;
printf ("please enter the student TP\n");
scanf ("%d",&deltp);
FILE *newfile =  fopen ("student_temp.txt","w");
while((fscanf(originalfile,"%i %s %s %s %d %d %d",&len, name, nationality, &gender, age, &TP, &contact))!=EOF){
    if(deltp != TP)
    fprintf(newfile,"%i\t %s\t %s\t %s\t %d\t ;%d;\t %d\n",strlen(name), name, nationality, gender, age, TP,contact);
}
fclose(newfile);
fclose(originalfile);