简体   繁体   English

如何使用 C append 是 a.txt 文件的第一行?

[英]How can I append the very first line of a .txt file using C?

I have a program where I am trying to add wind data to a.txt file, but I am facing the issue where I add data, I close the program, try to add data again, and it eliminates the rest of the data.我有一个程序,我试图将风数据添加到 a.txt 文件,但我面临添加数据的问题,我关闭程序,再次尝试添加数据,它消除了数据的 rest。 The following is an example of the.txt file:以下是 .txt 文件的示例:

3
3.60000 N
4.30000 E
5.40000 S

The first line is the number of data entries and the following are each wind speed and then wind direction.第一行是数据条目的数量,接下来是每个风速,然后是风向。

My code works fine if I just add the data once and be done with it, but not if I want to add more work to what I previously did.如果我只添加一次数据并完成它,我的代码可以正常工作,但如果我想在以前所做的工作中添加更多工作,则不行。 I know I can use append to add data to the end, but it will not update the counter at the top.我知道我可以使用 append 将数据添加到末尾,但它不会更新顶部的计数器。 I want to be able to add data and also update the counter at the top.我希望能够添加数据并更新顶部的计数器。 I need the counter for other functions in the code.我需要代码中其他功能的计数器。 The following is my code for the problem:以下是我的问题代码:

void addWindData(FILE* outFile, int numNumbers, double windSpd[], char windDir[]){
    int numItems;
    
    printf("How many data items would you like to add? ");
    scanf("%d", &numItems);

    fprintf(outFile, "%d \n", numItems);

    for(int i = 0; i < numItems; i++){
        printf("Wind speed? ");
        scanf("%lf", &windSpeed[i]);

        printf("Wind direction? ");
        scanf(" %c", &windDir[i]);

        fprintf(outFile, "%lf %c \n", windSpd[i], windDir[i]);
    }
}

I call the function as so:我这样称呼 function:

fp = fopen(FILE_NAME, "w");
if(fp == NULL){
    printf("File could not be found! \n");
}
else{
    addWindData(fp, numNumbers, windSpd, windDir);
    fclose(fp);
}

Any help I could get will be greatly appreciated.我能得到的任何帮助将不胜感激。 I understand that 'w' will always write, but when I use append, it starts a new count at the end and then adds the data.我知道'w'总是会写,但是当我使用 append 时,它会在最后开始一个新的计数,然后添加数据。 I just need help to figure out how to update the counter.我只需要帮助来弄清楚如何更新计数器。 I am very new to the C programming language and am still doing my best to learn, I am trying my best to use logic and reasoning, so it helps me to get inspiration from others!我对 C 编程语言非常陌生,并且仍在努力学习,我正在尽力使用逻辑和推理,因此它可以帮助我从他人那里获得灵感! Thank you in advance!先感谢您!

File is open as stream, so you have 2 choices: Change chars or rewrite all file.文件以 stream 的形式打开,因此您有 2 个选择:更改字符或重写所有文件。 You must know that you can change only existing chars!您必须知道您只能更改现有字符! As fopen reference says to change chars you can use "r+" mode.正如fopen 参考所说,要更改字符,您可以使用“r+”模式。 This mode allow to read/write, starts from beginning and allows to rewind .此模式允许读/写,从头开始并允许倒带

you can change fp = fopen(FILE_NAME, "w") to fp = fopen(FILE_NAME, "r+") because w mode will erase the previous text in.txt file but r+ do not because r is upgrade mode.您可以将fp = fopen(FILE_NAME, "w")更改为fp = fopen(FILE_NAME, "r+")因为 w 模式会擦除 .txt 文件中的先前文本,但 r+ 不会因为 r 是升级模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM