C++写一个txt文件插入删除行问题

网上找到一段代码,想改成自己的,老是有bug,求大神帮忙解决一下。

已经存在一个表了,想法是往表里加入一行,加到最后一行就行;另外,如果是要删除某一行的话,以第一列的学号为关键字,应该怎么写?

 #include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <Windows.h>

struct Student         //定义基本表——Student
{
    long long int Sno;      //学生学号
    char Sname[20];    //学生姓名
    char Ssex[6];      //学生性别
    int Sage;          //学生年龄
    char Sdept[20];    //学生所在系
};

int main()
{
    FILE *fp;
    char name[20];       //输入变量
    int sum;             //输入变量
    char fName[10][20];  //可存储10个人名
    int fScore[10];      //存储10个分数记录
    char buff1[20];
    char buff2[20];
    int i=0;
    //打开存储文件
    if ((fp=fopen("StudentData.txt","r"))==NULL)
    {
        printf("Can not open the file");
        getch();
        exit(0);
    }
    else
    {
        while (!feof(fp))
        {
            ZeroMemory(buff1,sizeof(buff1));       //清空内存
            ZeroMemory(buff2,sizeof(buff1));
            fgets(buff1,sizeof(buff1),fp);         //读取名称
            fgets(buff2,sizeof(buff2),fp);         //读取第二行分数
            if (strcmp(buff1,"")==0)
            {
                continue;
            }
            else
            {
                strcpy(fName[i],buff1);
                printf("%s",fName[i]);                 //输出名称
                fScore[i] = atoi(buff2);                //将字符型转换成int型
                printf("%i\n",fScore[i]);              //打印输出分数值
            }
            i++;
        }
    }
    fclose(fp);
    //打开存储文件,将排好序的数据重新写入文件中
    if ((fp=fopen("c:\\scorelist.txt","w"))==NULL)
    {
        printf("Can not open the file");
        getch();
        exit(0);
    }
    else
    {
        printf("Input the new name:\n");
        scanf("%s",name);
        printf("Input the new score:\n");
        scanf("%i",&sum);
        int j =0;
        //获取新增积分排序位置
        while(sum < fScore[j])
        {
             j++;

        }
        //移动数据重新对数组排序,从后往前排序
        int m = i;
        while (i>j)
        {
            strcpy(fName[i],fName[i-1]);
            fScore[i] = fScore[i-1];
            i--;
        }
        strcpy(fName[j],name);
        strcat(fName[j],"\n");
        fScore[j] = sum;
        //写入文本文件
        int k=0;
        while(k<=m)
        {
            fputs(fName[k],fp);
            fprintf(fp,"%i\n",fScore[k]);
            k++;
        }
    }
    fclose(fp);
}

这是txt文件里的内容。
2014010624 艾伦 男 19 IS
2014010413 郭宁 男 20 EN
2014010908 王凯 男 19 IS
2014021111 刘莉 女 19 EN
2014011536 陈军 男 21 IS
2014021314 赵爽 女 18 CA
2014010715 李宁 女 22 IS
2014010245 乔治 男 21 EN
2014021212 张雪 女 19 IS
2014021629 孙楠 男 23 CA
2014012231 马丁 男 20 CA

建议使用C++的文件IO流来读取文件,然后使用string的字符串查找方法string::find(char[])来查找学号相同的信息
例如:
int main()
{
//以读取流打开StudentData.txt文件
ifstream in("StudentData.txt");
string line;
string::size_type pos; // find()方法的返回值是 size_type 类型的
vector StudentDataVector; //申请一个string容器将数据保存起来
while(getline(in, line))
{
pos = line.find("2014011536",0);
if(pos < line.size())
{
//找到("2014011536")需要删除的不作处理
}
else
{
StudentDataVector.push_back(line); //将需要保留的数据装入容器中
}
}
//数据处理完成后关闭文件读取流
in.close();
//然后以写入流打开StudentData.txt文件
ofstream out("StudentData.txt");
//用一个for循环将刚才保存的数据重新写入文件
for(int i = 0 ; i < StudentDataVector.size() ; i++)
{
out << StudentDataVector[i];
}
//写入完成后关闭文件写入流
out.close();

return 0;

}

ofstream写不进txt文件的奇怪问题

建议使用C++的文件IO流来读取文件,然后使用string的字符串查找方法string::find(char[])来查找学号相同的信息
例如:
int main()
{
//以读取流打开StudentData.txt文件
ifstream in("StudentData.txt");
string line;
string::size_type pos; // find()方法的返回值是 size_type 类型的
vector StudentDataVector; //申请一个string容器将数据保存起来
while(getline(in, line))
{
pos = line.find("2014011536",0);
if(pos < line.size())
{
//找到("2014011536")需要删除的不作处理
}
else
{
StudentDataVector.push_back(line); //将需要保留的数据装入容器中
}
}
//数据处理完成后关闭文件读取流
in.close();
//然后以写入流打开StudentData.txt文件
ofstream out("StudentData.txt");
//用一个for循环将刚才保存的数据重新写入文件
for(int i = 0 ; i < StudentDataVector.size() ; i++)
{
out << StudentDataVector[i];
}
//写入完成后关闭文件写入流
out.close();

return 0;
}

有bug啊,怎么回事