Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 检查行,然后更改txt文件中的数据_C_File_Line_Edit - Fatal编程技术网

C 检查行,然后更改txt文件中的数据

C 检查行,然后更改txt文件中的数据,c,file,line,edit,C,File,Line,Edit,所以,我有这个: order.txt file.txt;10;21:21 file2.txt;3.20:00 file2.txt;30;22:20 file4.txt;3.10:00 我希望用户给出所需的行(1到3,取决于存在的行数),然后更改其内容。例如: 第3行 “文件名:File.txt” “秒:5” “时间HH:MM:20:00” 输出/更改为订单。txt: file.txt;10;21:21 file2.txt;3.20:20 file.txt;5.20:00 file4.txt;3.

所以,我有这个:

order.txt

file.txt;10;21:21

file2.txt;3.20:00

file2.txt;30;22:20

file4.txt;3.10:00

我希望用户给出所需的行(1到3,取决于存在的行数),然后更改其内容。例如:

第3行

文件名:File.txt”

秒:5”

时间HH:MM:20:00”

输出/更改为订单。txt:

file.txt;10;21:21

file2.txt;3.20:20

file.txt;5.20:00

file4.txt;3.10:00

如您所见,第三行发生了变化。为此,我尝试了以下方法:

变量

FILE* orderFile;
FILE* contentFile;
char fileName[50];
char timeValue[10];
int seconds;
char orderNameFile[50];
char orderTimeFile[50];
int orderSecondsFile;
int count = 0;
int i = 0;
int line;
int position;
代码

/* This will show the content on it with the number of the line behind */
orderFile = fopen("order.txt","r+");
                
printf("\n");

while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
    i++;
    printf("[%d] %s;%d;%s\n",i,fileName,seconds,timeValue);
}

printf("\n");

rewind(orderFile);
/* ******************************************************************* */

/* This will ask which line he wants to change */
do{

    printf("Choose the line you want to change\n");
    printf("[Line] ");
    scanf("%d",&line);
    printf("\n");

    if(line > i)
    {
         printf("That line does not exist\n\n");
    }

}while(line > i || line < 1);
/* ******************************************* */

/* This will ask for the file name, seconds and time in HH:MM */
printf("Insert the name of the file\n");
printf("[Name] ");
getchar();
scanf("%s",&orderNameFile);

printf("Insert the presentation time in seconds\n");
printf("[Time in seconds] ");
scanf("%d",&orderSecondsFile);

printf("Insert the hour it starts\n");
printf("[Time in HH:MM (Hour:Minutes)] ");
getchar();
scanf("%s%",&orderTimeFile);
/* ******************************************* */

/* This is what is supposed to edit the line I want but does not work */
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
    count++;

    if(count == line)
    {
        position = ftell(orderFile);
        fseek(orderFile, position, SEEK_SET);
        fprintf(orderFile,"%s;%d;%s",orderNameFile,orderSecondsFile,orderTimeFile);
        break;
    }
}
*/ ****************************************************************** */

fclose(orderFile);
/*这将显示其上的内容以及后面的行号*/
orderFile=fopen(“order.txt”、“r+”);
printf(“\n”);
而(fscanf(orderFile,“%49[^;]”;%d;%49[^\n]”,文件名,&seconds,timeValue)==3)
{
i++;
printf(“[%d]%s;%d;%s\n”,i,文件名,秒数,时间值);
}
printf(“\n”);
倒带(订单文件);
/* ******************************************************************* */
/*这将询问他想换哪一行*/
做{
printf(“选择要更改的行\n”);
printf(“[行]”);
scanf(“%d”行和“&line”);
printf(“\n”);
如果(行>i)
{
printf(“该行不存在\n\n”);
}
}而(直线>i | |直线<1);
/* ******************************************* */
/*这将要求输入文件名、秒数和时间,单位为HH:MM*/
printf(“插入文件名”);
printf(“[名称]”);
getchar();
scanf(“%s”和&orderNameFile);
printf(“以秒为单位插入演示时间\n”);
printf(“[以秒为单位的时间]”);
scanf(“%d”、&orderssecondsfile);
printf(“插入它开始的时间\n”);
printf(“[时间单位为小时:毫米(小时:分钟)]”;
getchar();
scanf(“%s%”,&orderTimeFile);
/* ******************************************* */
/*这是什么应该编辑线,我想要的,但不工作*/
而(fscanf(orderFile,“%49[^;]”;%d;%49[^\n]”,文件名,&seconds,timeValue)==3)
{
计数++;
如果(计数==行)
{
position=ftell(订单文件);
fseek(订单文件、位置、搜索集);
fprintf(orderFile,“%s;%d;%s”、orderNameFile、orderSecondsFile、orderTimeFile);
打破
}
}
*/ ****************************************************************** */
fclose(订单文件);
使用第一个示例我想要的输出:

file.txt;10;21:21

file2.txt;3.20:20

file.txt;5.20:00

file4.txt;3.10:00

使用第一个示例@EDITED:

file.txt;10;21:21

file2.txt;3.20:00

file2.txt;30;22:20file.txt;5.20:00

file4.txt;3.10:00


有什么想法吗?

您必须存储行,然后将它们重写到文件中,可能会有更优雅/高效的解决方案,但对于一个简单的情况,这应该是可以的,下面是代码

#include <stdio.h>
#include <string.h>

int main()
{
    FILE* orderFile;
    char fileName[50];
    char timeValue[10];
    int seconds;
    char orderNameFile[50];
    char orderTimeFile[50];
    int orderSecondsFile;
    int count;
    int lineCount;
    int line;
    char lineContent[128];
    char fileLines[128][128];

    /* This will show the content on it with the number of the line behind */
    orderFile = fopen("order.txt", "r+");
    if (orderFile == NULL)
        return -1;
    lineCount = 0;

    while ((fgets(lineContent, sizeof(lineContent), orderFile) != NULL) && (lineCount < 128))
    {
        size_t length;

        length = strlen(lineContent);
        if (lineContent[length - 1] == '\n')
            lineContent[length - 1] = 0;
        if (sscanf(lineContent, " %49[^;];%d; %49[^\n]", fileName, &seconds, timeValue) == 3)
        {
            strcpy(fileLines[lineCount], lineContent);
            printf("[%d] %s;%d;%s\n", ++lineCount, fileName, seconds, timeValue);
        }
    }
    printf("\n");

    rewind(orderFile);

    /* This will ask which line he wants to change */
    do {

        printf("Choose the line you want to change\n");
        printf("[Line] > ");

        scanf("%d", &line);

        if ((line > lineCount) || (line < 1))
             printf("That line does not exist\n\n");
    } while ((line > lineCount) || (line < 1));

    /* This will ask for the file name, seconds and time in HH:MM */
    printf("Insert the name of the file\n");
    printf("[Name] ");

    scanf("%49s", orderNameFile);

    printf("Insert the presentation time in seconds\n");
    printf("[Time in seconds] ");

    scanf("%d", &orderSecondsFile);

    printf("Insert the hour it starts\n");
    printf("[Time in HH:MM (Hour:Minutes)] ");

    scanf("%49s", orderTimeFile);

    /* This is what is supposed to edit the line I want but does not work */
    count = 0;
    while (++count < lineCount + 1)
    {
        if (count == line)
            fprintf(orderFile, "%s;%d;%s\n", orderNameFile, orderSecondsFile, orderTimeFile);
        else
            fprintf(orderFile, "%s\n", fileLines[count - 1]);
    }
    fclose(orderFile);

    return 0;
}
#包括
#包括
int main()
{
文件*订单文件;
字符文件名[50];
字符时间值[10];
整数秒;
char orderNameFile[50];
char orderTimeFile[50];
int orderssecondsfile;
整数计数;
整数行数;
内线;
char-lineContent[128];
字符文件行[128][128];
/*这将显示其上的内容以及后面的行数*/
orderFile=fopen(“order.txt”、“r+”);
if(orderFile==NULL)
返回-1;
行数=0;
而((fgets(lineContent,sizeof(lineContent),orderFile)!=NULL)和&(lineCount<128))
{
尺寸与长度;
长度=strlen(线条内容);
如果(线条内容[长度-1]='\n')
线条内容[长度-1]=0;
if(sscanf(lineContent,“%49[^;]”;%d;%49[^\n]”,文件名,&seconds,timeValue)==3)
{
strcpy(文件行[lineCount],lineContent);
printf(“[%d]%s;%d;%s\n”++行数、文件名、秒数、时间值);
}
}
printf(“\n”);
倒带(订单文件);
/*这将询问他想换哪一行*/
做{
printf(“选择要更改的行\n”);
printf(“[行]>”);
scanf(“%d”行和“&line”);
如果((行>行计数)| |(行<1))
printf(“该行不存在\n\n”);
}而((行>行数)| |(行<1));
/*这将要求输入文件名、秒数和时间,单位为HH:MM*/
printf(“插入文件名”);
printf(“[名称]”);
scanf(“%49s”,orderNameFile);
printf(“以秒为单位插入演示时间\n”);
printf(“[以秒为单位的时间]”);
scanf(“%d”、&orderssecondsfile);
printf(“插入它开始的时间\n”);
printf(“[时间单位为小时:毫米(小时:分钟)]”;
scanf(“%49s”,订单时间文件);
/*这是什么应该编辑线,我想要的,但不工作*/
计数=0;
而(++count
通常,最好先写入临时文件,然后删除原始文件并将临时文件重命名为原始文件的名称

FILE *temp = fopen ( "temp", "w");
/* This is what is supposed to edit the line I want but does not work */
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
    count++;

    if(count == line)
    {
        fprintf(temp,"%s;%d;%s\n",orderNameFile,orderSecondsFile,orderTimeFile);
    }
    else 
    {
        fprintf(temp,"%s;%d;%s\n",filename,seconds,timeValue);
    }
}
fclose ( temp);
fclose ( orderFile);
remove ( "order.txt");// comment these two lines out until you are sure temp is correct
rename ( "temp", "order.txt");//that way you do not loose the original file

删除文件的内容是一项代价高昂的操作处理此问题的更好方法是

  • 以写入模式打开新文件
  • 开始将以前文件的内容写入此新文件
  • 到达应替换的行后,请确保忽略读取行,并使用
    fprintf()
  • 在我看来,这是一种更简单、更正确的方法。使用下面的线程作为参考


    这似乎是与变量linha有关的问题。我看不出它的任何定义,所以我很难说问题出在哪里