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. Reading a text file

Reading a text file

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 6 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.
  • M Martial Spirit

    sure, It isn't a difficult thing:-) read the text to buffer, and chr the space char ' ', then replace it with '\r', OK, output it to file or screen. Best Regards

    A Offline
    A Offline
    Andrew Admire
    wrote on last edited by
    #4

    Is it still possible with the console app? I would like to output it to a new file.

    D 1 Reply Last reply
    0
    • A Andrew Admire

      Is it still possible with the console app? I would like to output it to a new file.

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

      Andrew Admire wrote: Is it still possible with the console app? Of course. Andrew Admire wrote: I would like to output it to a new file. Not a problem. Is this an MFC-based application?


      "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

      A 1 Reply Last reply
      0
      • D David Crow

        Andrew Admire wrote: Is it still possible with the console app? Of course. Andrew Admire wrote: I would like to output it to a new file. Not a problem. Is this an MFC-based application?


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        A Offline
        A Offline
        Andrew Admire
        wrote on last edited by
        #6

        I do not know how to use MFC so no, I would like it to be straight console app based.

        D 1 Reply Last reply
        0
        • A Andrew Admire

          I do not know how to use MFC so no, I would like it to be straight console app based.

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

          Andrew Admire wrote: ...I would like it to be straight console app based. MFC can be used in both GUI and console applications.


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

          A 1 Reply Last reply
          0
          • D David Crow

            Andrew Admire wrote: ...I would like it to be straight console app based. MFC can be used in both GUI and console applications.


            "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

            A Offline
            A Offline
            Andrew Admire
            wrote on last edited by
            #8

            Ok, then no I would like to use C++.

            D 1 Reply Last reply
            0
            • A Andrew Admire

              Ok, then no I would like to use C++.

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

              This should get you close. You can make the necessary changes and organize it a bit better.

              FILE *pFile = fopen("C:\\!conMania Collection\\list71.txt", "rb");
              if (NULL != pFile)
              {
              // how big is the file?
              fseek(pFile, 0, SEEK_END);
              long lSize = ftell(pFile);
              fseek(pFile, 0, SEEK_SET);

              // allocate a buffer for it
              char \*pBuffer = new char\[lSize\];
               
              // read the entire file
              // much more efficient than reading one byte at a time
              fread(pBuffer, sizeof(char), lSize, pFile);
              fclose(pFile);
               
              // change the spaces to linefeeds
              for (int x = 0; x < lSize; x++)
              {
                  if (pBuffer\[x\] == ' ')
                      pBuffer\[x\] = '\\n';
              }
               
              // create a new file
              pFile = fopen("C:\\\\!conMania Collection\\\\list71\_temp.txt", "wb");
              if (NULL != pFile)
              {
                  fwrite(pBuffer, sizeof(char), lSize, pFile);
                  fclose(pFile);
              }
               
              delete \[\] pBuffer;
              

              }


              "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

              A 1 Reply Last reply
              0
              • A Andrew Admire

                Is there a way to create a program that would read a text file and every time it found a space it would replace the space with a line return?

                C Offline
                C Offline
                cgb143
                wrote on last edited by
                #10

                this needs a lot of polish, but it does the bare essentials. in_filename and out_filename could be selected using CFileDialog. they are hard coded here. #include windows.h #include string.h #include stdio.h main() { FILE *infile_p; // pointer for read file FILE *outfile_p; // pointer for write file int byte;// var for byte read from file if ((infile_p = fopen("in_filename", "r"))==NULL) // open file for read { exit(0); } if ((outfile_p = fopen("out_filename", "w"))==NULL) // open file for write { exit(0); } while (!feof(infile_p)) // end of file test { byte = (fgetc(infile_p)); if (byte == 32) // look for a space { fprintf(outfile_p ,"%c%c", 0x0d, 0x0a);/ replace it with CR/LF } else { fprintf( outfile_p, "%c", byte); } } return(0); }

                A 1 Reply Last reply
                0
                • D David Crow

                  This should get you close. You can make the necessary changes and organize it a bit better.

                  FILE *pFile = fopen("C:\\!conMania Collection\\list71.txt", "rb");
                  if (NULL != pFile)
                  {
                  // how big is the file?
                  fseek(pFile, 0, SEEK_END);
                  long lSize = ftell(pFile);
                  fseek(pFile, 0, SEEK_SET);

                  // allocate a buffer for it
                  char \*pBuffer = new char\[lSize\];
                   
                  // read the entire file
                  // much more efficient than reading one byte at a time
                  fread(pBuffer, sizeof(char), lSize, pFile);
                  fclose(pFile);
                   
                  // change the spaces to linefeeds
                  for (int x = 0; x < lSize; x++)
                  {
                      if (pBuffer\[x\] == ' ')
                          pBuffer\[x\] = '\\n';
                  }
                   
                  // create a new file
                  pFile = fopen("C:\\\\!conMania Collection\\\\list71\_temp.txt", "wb");
                  if (NULL != pFile)
                  {
                      fwrite(pBuffer, sizeof(char), lSize, pFile);
                      fclose(pFile);
                  }
                   
                  delete \[\] pBuffer;
                  

                  }


                  "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #11

                  Don't you need replace with a \r\n ?

                  D 1 Reply Last reply
                  0
                  • A Anonymous

                    Don't you need replace with a \r\n ?

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

                    I don't know. It's not my project. Given that \r is a carriage return and \n is a line feed, I'm not sure what a "line return" is.


                    "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                    1 Reply Last reply
                    0
                    • A Andrew Admire

                      Is there a way to create a program that would read a text file and every time it found a space it would replace the space with a line return?

                      B Offline
                      B Offline
                      beerboy_22
                      wrote on last edited by
                      #13

                      Here's a C++ solution: #include #include using namespace std; ... ifstream in("in.txt"); ofstream out("out.txt"); string s; while(is >> s) os << s << endl;

                      1 Reply Last reply
                      0
                      • C cgb143

                        this needs a lot of polish, but it does the bare essentials. in_filename and out_filename could be selected using CFileDialog. they are hard coded here. #include windows.h #include string.h #include stdio.h main() { FILE *infile_p; // pointer for read file FILE *outfile_p; // pointer for write file int byte;// var for byte read from file if ((infile_p = fopen("in_filename", "r"))==NULL) // open file for read { exit(0); } if ((outfile_p = fopen("out_filename", "w"))==NULL) // open file for write { exit(0); } while (!feof(infile_p)) // end of file test { byte = (fgetc(infile_p)); if (byte == 32) // look for a space { fprintf(outfile_p ,"%c%c", 0x0d, 0x0a);/ replace it with CR/LF } else { fprintf( outfile_p, "%c", byte); } } return(0); }

                        A Offline
                        A Offline
                        Anonymous
                        wrote on last edited by
                        #14

                        Your program worked. However it kept the space after the . at the end of the sentence.

                        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