简体   繁体   中英

How to make 2 file pointers write in one file sequentially in C++?

#include <QCoreApplication>
#include <stdio.h>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int x = 2;

    FILE *fp [x];

    fp[0] = fopen ("temp.txt", "w");
    fp[1] = fopen ("temp.txt", "w");

    if (fp[1])
        qDebug () << "Fine";

    fputs ("What is your name?\n", fp[0]);
    fputs ("What is your name?\n", fp[0]);
    fputs ("What is your name?\n", fp[0]);

    fputs ("My name is XYZ.\n", fp[1]);

    fputs ("What is your name?\n", fp[0]);
    fputs ("What is your name?\n", fp[0]);


    fclose (*fp);

    return a.exec();
}

I am using Qt for the convenience sake.

Word "Fine" is being printed.
The output of this program is:

What is your name?
What is your name?
What is your name?
What is your name?
What is your name?

Why is My name is XYZ. missing from the output? What can be done to make these file pointers write sequentially in one file?

Borrowing from Opening a file using fopen with same flag in C

You have 2 FILE* open to the same file, pointing at the beginning of the file, so one of the writes overwrites the other.

Note also that FILE* are normally buffered, so these small strings actually gets written to the file when you fclose() or fflush() the FILE*. Since you do neither, the system will do it when the application exits , so which write gets overwritten depends on which file the system closes first.

If you open the 2 files in append mode , fopen("mytext","a");, you'll see different result, but you'll need to fflush() the FILE* when you want to make sure operating on the other FILE* doesn't cause interleaved output. And writing to the same file from different processes/threads will take more care, eg some form of file locking.

In your case you are closing fp[0] but not fp[1] . Both writes are independent and possibly "My name is XYZ.\\n" is flushed into the file first (to the beginning) and then all the string "What is your name?\\n" are flushed into the file from the beginning overwriting previous line.

How to make 2 file pointers write in one file sequentially in C++?

Don't reopen the file using fopen and use the same file descriptor or use a log utility function which keeps a static pointer to opened file or open the file everytime you log and closes the file after logging.

static void log(char *line)
{
  static FILE *fp = fopen("temp.txt", "w");
  if(fp) fputs ("What is your name?\n", fp);
}

and later in main()

log("What is your name\n");
FILE *fp [x];

fp[0] = fopen ("temp.txt", "w");

fputs ("What is your name?\n", fp[0]);
fputs ("What is your name?\n", fp[0]);
fputs ("What is your name?\n", fp[0]);
fputs ("What is your name?\n", fp[0]);
fputs ("What is your name?\n", fp[0]);
fp[1] = fopen ("temp.txt", "w");
fputs ("My name is XYZ.\n", fp[1]);

Try like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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