Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Files

Files

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chaitanya22
    wrote on last edited by
    #1

    hi, I need to open an text file to save the output data from the machine, i could open the text file and could write the output data to the text file, but the problem is how could i save the previous test data output if i again run the program? file = fopen("output.txt", "w+"); ... fputs(txt, file); fputs("\n", file); If i run the program, i could write the data of the present running data output, i need to save the old output data for the checking the drift? Can i use rewind???? Thnaks in advance

    R K 2 Replies Last reply
    0
    • C chaitanya22

      hi, I need to open an text file to save the output data from the machine, i could open the text file and could write the output data to the text file, but the problem is how could i save the previous test data output if i again run the program? file = fopen("output.txt", "w+"); ... fputs(txt, file); fputs("\n", file); If i run the program, i could write the data of the present running data output, i need to save the old output data for the checking the drift? Can i use rewind???? Thnaks in advance

      R Offline
      R Offline
      Ray Kinsella
      wrote on last edited by
      #2

      use file = fopen("output.txt", "a+"); to append, see function description in MSDN. Regards Ray "Je Suis Mort De Rire" Blogging @ Keratoconus Watch

      1 Reply Last reply
      0
      • C chaitanya22

        hi, I need to open an text file to save the output data from the machine, i could open the text file and could write the output data to the text file, but the problem is how could i save the previous test data output if i again run the program? file = fopen("output.txt", "w+"); ... fputs(txt, file); fputs("\n", file); If i run the program, i could write the data of the present running data output, i need to save the old output data for the checking the drift? Can i use rewind???? Thnaks in advance

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #3

        Open the file for append, "a(+)" instead of "w+". "w+" always creates the file. "a" creates the file if it doesn't exist. Else, it adds to at the (previous) end of the file.

        C 1 Reply Last reply
        0
        • K kakan

          Open the file for append, "a(+)" instead of "w+". "w+" always creates the file. "a" creates the file if it doesn't exist. Else, it adds to at the (previous) end of the file.

          C Offline
          C Offline
          chaitanya22
          wrote on last edited by
          #4

          ya thankyou, it is working!! Its appending from the end of the previous data file, like 1 // 1 2 3 4 .......one data output 2 3 4 5 //5 6 7 8.....another data output 6 7 8 if i would like to have as, 1 2 3 4 //as first data output nad the next file output should begin in new line as 1 5 2 6 3 7 4 8.. hwo could i proceed??i would like tos tart in new line again

          K 1 Reply Last reply
          0
          • C chaitanya22

            ya thankyou, it is working!! Its appending from the end of the previous data file, like 1 // 1 2 3 4 .......one data output 2 3 4 5 //5 6 7 8.....another data output 6 7 8 if i would like to have as, 1 2 3 4 //as first data output nad the next file output should begin in new line as 1 5 2 6 3 7 4 8.. hwo could i proceed??i would like tos tart in new line again

            K Offline
            K Offline
            kakan
            wrote on last edited by
            #5

            You mean a new column. Well, that's not easy to do. Considder the data in the file (from the first run). It's something like this: 1 2 3 4 Now you want to insert characters (" 5") in the first line, after "1" and before the character. To be able to do that (in the simplest way), you will have to create a new file. Then read from the old file, one line at the time, modify that line and then write the modified line to the new file. When the new file is done, delete the old file and rename the new file to the old files name. Not very easy for a beginner. Can you considder another principle?

            C 1 Reply Last reply
            0
            • K kakan

              You mean a new column. Well, that's not easy to do. Considder the data in the file (from the first run). It's something like this: 1 2 3 4 Now you want to insert characters (" 5") in the first line, after "1" and before the character. To be able to do that (in the simplest way), you will have to create a new file. Then read from the old file, one line at the time, modify that line and then write the modified line to the new file. When the new file is done, delete the old file and rename the new file to the old files name. Not very easy for a beginner. Can you considder another principle?

              C Offline
              C Offline
              chaitanya22
              wrote on last edited by
              #6

              Helo Mr. Kakan, Is it possible to do so 1 2 3 4 5 6 7 8 writing the new data in another next line???

              K 1 Reply Last reply
              0
              • C chaitanya22

                Helo Mr. Kakan, Is it possible to do so 1 2 3 4 5 6 7 8 writing the new data in another next line???

                K Offline
                K Offline
                kakan
                wrote on last edited by
                #7

                Hello chaitanya. That's much easier. That way, you just append to the file. Just make sure you put a '\n' character ONLY at the end of line. Today, you write a '\n' for each value, which gives a newline after each value. That's the reason why you get each value on a separate text line in the file. I don't know what function you are using for writing to the file today, but i suspect you are using fputc() or fputs(), right? There are numerous ways to write to a file, this is just one way. You can to this, using fputc. (I assume FILE * file): int number = '1'; fputc(number, file); fputc((int) ' ', file); number = '2'; fputc(number, file); fputc((int) ' ', file); number = '3'; fputc(number, file); fputc((int) ' ', file); And the last number: fputc(number, file); fputc((int) '\n', file); Or, if you got all numbers at the same time, you can do this: fprintf(file, "%d %d %d %d\n", 1,2,3,4); fprintf(file, "%d %d %d %d\n", 5,6,7,8); This is very basic knowledge, I think you should read a good beginners book about C and then go on with a book about C++. Good luck Kakan

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups