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. Read This

Read This

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 Posts 4 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.
  • L Offline
    L Offline
    LOSTTWARE com
    wrote on last edited by
    #1

    Newbie Question. Ok, I need to write and then latter read multiple lines to a file. I figured out how to write one line to a file, and I think I am close for multiple lines, but I am just doin something wrong. char test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 test2); fclose(fp); } Well, obviously that isn't working. Latter it is going to be for like 5 vars. So I am going to need to know how to write five vars, and read five vars to and from a file. I think for reading it will be something like this. FILE *fp=fopen("Test.startup","r"); if(fp){ int SomeVar; if(fscanf(fp,"%d",&SomeVar)==1){ // Some Code } fclose(fp); } Please help. Thanks, Josh

    L E J 3 Replies Last reply
    0
    • L LOSTTWARE com

      Newbie Question. Ok, I need to write and then latter read multiple lines to a file. I figured out how to write one line to a file, and I think I am close for multiple lines, but I am just doin something wrong. char test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 test2); fclose(fp); } Well, obviously that isn't working. Latter it is going to be for like 5 vars. So I am going to need to know how to write five vars, and read five vars to and from a file. I think for reading it will be something like this. FILE *fp=fopen("Test.startup","r"); if(fp){ int SomeVar; if(fscanf(fp,"%d",&SomeVar)==1){ // Some Code } fclose(fp); } Please help. Thanks, Josh

      L Offline
      L Offline
      LOSTTWARE com
      wrote on last edited by
      #2

      char test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 | test2); fclose(fp); } Well that didn't work, the file ended up being "3" darn.:(( LOSTTWARE.com

      1 Reply Last reply
      0
      • L LOSTTWARE com

        Newbie Question. Ok, I need to write and then latter read multiple lines to a file. I figured out how to write one line to a file, and I think I am close for multiple lines, but I am just doin something wrong. char test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 test2); fclose(fp); } Well, obviously that isn't working. Latter it is going to be for like 5 vars. So I am going to need to know how to write five vars, and read five vars to and from a file. I think for reading it will be something like this. FILE *fp=fopen("Test.startup","r"); if(fp){ int SomeVar; if(fscanf(fp,"%d",&SomeVar)==1){ // Some Code } fclose(fp); } Please help. Thanks, Josh

        E Offline
        E Offline
        Ernesto D
        wrote on last edited by
        #3

        Why dont you try with fstream instead? i find them to be a lot easier to use. if you need help using fstreams, take a look in here http://www.cpp-home.com/FileIO_tutorial.php its a good tutorial on them. hope this helps

        L 1 Reply Last reply
        0
        • E Ernesto D

          Why dont you try with fstream instead? i find them to be a lot easier to use. if you need help using fstreams, take a look in here http://www.cpp-home.com/FileIO_tutorial.php its a good tutorial on them. hope this helps

          L Offline
          L Offline
          LOSTTWARE com
          wrote on last edited by
          #4

          Ok, I am getting closer, still getting errors, but so far I have this, ifstream OpenFile("VARS.startup"); char line[6]; while(!OpenFile.eof()) { OpenFile.getline(line,6); // cout << ch; var_toolbar1 << line; var_toolbar2 << line; var_toolbar3 << line; var_toolbar4 << line; var_LCLWindow << line; var_OUTWindow << line; } OpenFile.close(); I declared the vars as intergers in the header file, and each line contains a 0 or a 1. The file has 6 lines for now. I need to get each line into it's respectful Var. LOSTTWARE.com

          1 Reply Last reply
          0
          • L LOSTTWARE com

            Newbie Question. Ok, I need to write and then latter read multiple lines to a file. I figured out how to write one line to a file, and I think I am close for multiple lines, but I am just doin something wrong. char test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 test2); fclose(fp); } Well, obviously that isn't working. Latter it is going to be for like 5 vars. So I am going to need to know how to write five vars, and read five vars to and from a file. I think for reading it will be something like this. FILE *fp=fopen("Test.startup","r"); if(fp){ int SomeVar; if(fscanf(fp,"%d",&SomeVar)==1){ // Some Code } fclose(fp); } Please help. Thanks, Josh

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5
            1. fprintf(fp,"%d",test1 test2); How did this compile? If the line was "fprintf(fp,"%d",test1, test2);" then it would make since. OH by the way: fopen("Test.startup","w") and fopen("Test.startup","r") assumes that you are loading binary data not text data. Use fopen("Test.startup","wt") and fopen("Test.startup","rt") when working with text file. If you want to store characters '1' and '2' to a file:

            char test1 = '1';
            char test2 = '2';
            FILE *fp = fopen("Test.startup","wt");
            if(fp)
            {
            fprintf(fp,"%c%c",test1, test2);
            fclose(fp);
            }

            If you want to load characters '1' and '2' from a file:

            char test1;
            char test2;

            FILE *fp=fopen("Test.startup","rt");
            if(fp)
            {
            if( fscanf(fp,"%c%c", &test1, &test2) )
            {
            // Some Code
            }
            fclose(fp);
            }

            From what I have seen, all you need to do is study the documentation for file I/O. I know peaple ar going to throw iostream a you as a suposably better solution. But 90% of the time it is just a wrapper for for what you are doing. Writing chacaters to a file using fprintf() is ok, but reading them with fsprintf() has (almost always) been a joke. Before you use iostream you must understand reading a file at the system level (a.k.a. C level). INTP

            D 1 Reply Last reply
            0
            • J John R Shaw
              1. fprintf(fp,"%d",test1 test2); How did this compile? If the line was "fprintf(fp,"%d",test1, test2);" then it would make since. OH by the way: fopen("Test.startup","w") and fopen("Test.startup","r") assumes that you are loading binary data not text data. Use fopen("Test.startup","wt") and fopen("Test.startup","rt") when working with text file. If you want to store characters '1' and '2' to a file:

              char test1 = '1';
              char test2 = '2';
              FILE *fp = fopen("Test.startup","wt");
              if(fp)
              {
              fprintf(fp,"%c%c",test1, test2);
              fclose(fp);
              }

              If you want to load characters '1' and '2' from a file:

              char test1;
              char test2;

              FILE *fp=fopen("Test.startup","rt");
              if(fp)
              {
              if( fscanf(fp,"%c%c", &test1, &test2) )
              {
              // Some Code
              }
              fclose(fp);
              }

              From what I have seen, all you need to do is study the documentation for file I/O. I know peaple ar going to throw iostream a you as a suposably better solution. But 90% of the time it is just a wrapper for for what you are doing. Writing chacaters to a file using fprintf() is ok, but reading them with fsprintf() has (almost always) been a joke. Before you use iostream you must understand reading a file at the system level (a.k.a. C level). INTP

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              John R. Shaw wrote: Writing chacaters to a file using fprintf() is ok, but reading them with fsprintf() has (almost always) been a joke. Someone has actually suggested that reading from a file can be done via fsprintf()?


              Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

              J 1 Reply Last reply
              0
              • D David Crow

                John R. Shaw wrote: Writing chacaters to a file using fprintf() is ok, but reading them with fsprintf() has (almost always) been a joke. Someone has actually suggested that reading from a file can be done via fsprintf()?


                Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #7

                Doh!:-O I was not thinking clearly. I just made some modifications to the sample code that was given. Besides it should not have been fsprintf (does this exist), it should have been fscanf. INTP

                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