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. File format [modified]

File format [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
ioshelpquestion
5 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.
  • D Offline
    D Offline
    dellthinker
    wrote on last edited by
    #1

    Hi all. Im having trouble making this file read-out look nice. The code is this: #include using namespace std; int main(){ char buffer[100]; ifstream file("file.dat",ios::end); if(file.fail()){ printf("File not found.\n"); } printf("File contents read the following:\n"); while(file >> buffer){ printf("\n%s ",buffer); } file.close(); system("pause"); return 0; } The file "file.dat" reads "This is only a test" inside. But when i run the program it reads: File contents read the following: This is only a test ----------------------- So i figured maybe it was a problem with my way of printing the message in the console. So i switched the printf around from this printf("\n%s ",buffer); To this printf("%s\n ",buffer); To this printf("%s",buffer); // This putsallthewordstogetherlikeso And still cant come up with a better looking format. Is there another way i can make it print the way its saved in the file? Thanx in advance! P.S. If i made the print like this: printf("%s ",buffer); It would print the first line in the file as its written in the file. But if there were two lines or three etc. It prints it like this "This is only a test This is only a test This is only a test" Which isnt what im looking to do. I'd like it look something like This is only a test This is only a test This is only a test

    R D M 3 Replies Last reply
    0
    • D dellthinker

      Hi all. Im having trouble making this file read-out look nice. The code is this: #include using namespace std; int main(){ char buffer[100]; ifstream file("file.dat",ios::end); if(file.fail()){ printf("File not found.\n"); } printf("File contents read the following:\n"); while(file >> buffer){ printf("\n%s ",buffer); } file.close(); system("pause"); return 0; } The file "file.dat" reads "This is only a test" inside. But when i run the program it reads: File contents read the following: This is only a test ----------------------- So i figured maybe it was a problem with my way of printing the message in the console. So i switched the printf around from this printf("\n%s ",buffer); To this printf("%s\n ",buffer); To this printf("%s",buffer); // This putsallthewordstogetherlikeso And still cant come up with a better looking format. Is there another way i can make it print the way its saved in the file? Thanx in advance! P.S. If i made the print like this: printf("%s ",buffer); It would print the first line in the file as its written in the file. But if there were two lines or three etc. It prints it like this "This is only a test This is only a test This is only a test" Which isnt what im looking to do. I'd like it look something like This is only a test This is only a test This is only a test

      R Offline
      R Offline
      Reagan Conservative
      wrote on last edited by
      #2

      I think you may find this link very useful www.dorota.ecs.fullerton.edu/classes/CS231/lect3-cs.ppt or Google on "ios:end"

      John P.

      D 1 Reply Last reply
      0
      • R Reagan Conservative

        I think you may find this link very useful www.dorota.ecs.fullerton.edu/classes/CS231/lect3-cs.ppt or Google on "ios:end"

        John P.

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

        jparken wrote:

        www.dorota.ecs.fullerton.edu/classes/CS231/lect3-cs.ppt

        Is http://www.dorota.ecs.fullerton.edu/ a valid domain?


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        1 Reply Last reply
        0
        • D dellthinker

          Hi all. Im having trouble making this file read-out look nice. The code is this: #include using namespace std; int main(){ char buffer[100]; ifstream file("file.dat",ios::end); if(file.fail()){ printf("File not found.\n"); } printf("File contents read the following:\n"); while(file >> buffer){ printf("\n%s ",buffer); } file.close(); system("pause"); return 0; } The file "file.dat" reads "This is only a test" inside. But when i run the program it reads: File contents read the following: This is only a test ----------------------- So i figured maybe it was a problem with my way of printing the message in the console. So i switched the printf around from this printf("\n%s ",buffer); To this printf("%s\n ",buffer); To this printf("%s",buffer); // This putsallthewordstogetherlikeso And still cant come up with a better looking format. Is there another way i can make it print the way its saved in the file? Thanx in advance! P.S. If i made the print like this: printf("%s ",buffer); It would print the first line in the file as its written in the file. But if there were two lines or three etc. It prints it like this "This is only a test This is only a test This is only a test" Which isnt what im looking to do. I'd like it look something like This is only a test This is only a test This is only a test

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

          dellthinker wrote:

          Is there another way i can make it print the way its saved in the file?

          Yes. Either read and print it on a line-by-line basis, or read each word into an array and print the contents of the array as you see fit.


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          1 Reply Last reply
          0
          • D dellthinker

            Hi all. Im having trouble making this file read-out look nice. The code is this: #include using namespace std; int main(){ char buffer[100]; ifstream file("file.dat",ios::end); if(file.fail()){ printf("File not found.\n"); } printf("File contents read the following:\n"); while(file >> buffer){ printf("\n%s ",buffer); } file.close(); system("pause"); return 0; } The file "file.dat" reads "This is only a test" inside. But when i run the program it reads: File contents read the following: This is only a test ----------------------- So i figured maybe it was a problem with my way of printing the message in the console. So i switched the printf around from this printf("\n%s ",buffer); To this printf("%s\n ",buffer); To this printf("%s",buffer); // This putsallthewordstogetherlikeso And still cant come up with a better looking format. Is there another way i can make it print the way its saved in the file? Thanx in advance! P.S. If i made the print like this: printf("%s ",buffer); It would print the first line in the file as its written in the file. But if there were two lines or three etc. It prints it like this "This is only a test This is only a test This is only a test" Which isnt what im looking to do. I'd like it look something like This is only a test This is only a test This is only a test

            M Offline
            M Offline
            Michal Zalewski
            wrote on last edited by
            #5

            Operator >> used with (i/o)fstream read one word from actual position in file to first space or end line. If you want read line you must use getline. You can read about it there: http://www.cplusplus.com/istream::getline :)

            ----------------------------------------------------------------- Surely without war there would be no loss Hence no mourning, no grief, no pain, no misery No sleepless nights missing the dead... Oh, no more No more war [Sleepless - Cradle of Filth]

            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