Text File Manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compman9902
    New Member
    • Mar 2007
    • 105

    Text File Manipulation

    Hello, and thank you for taking the time to read this post. Here is my problem: every time i try to open up a text file, in a program, and try to add something to the end of it on a new line, then it erases the entire file and only puts in the single line. Thank you very much in advance for your help and dedication to these forums.



    --Parker Woods
    --MonsterHackz DEV Team
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    Then it means u are opening the file in write mode.
    Open the file in append mode if u want add any info o open in read mode if u only want to read info.
    Thanks
    Raghuram

    Comment

    • compman9902
      New Member
      • Mar 2007
      • 105

      #3
      Originally posted by gpraghuram
      Hi,
      Then it means u are opening the file in write mode.
      Open the file in append mode if u want add any info o open in read mode if u only want to read info.
      Thanks
      Raghuram
      That was very......helpf ul
      could I please have some code with that?
      just, use this example:
      Code:
      ofstream filesave ("C:\\my text file.txt");
      if (filesave.is_open())
      {
      filesave << testToBeAdded;
      filesave.close();
      }
      else
      {
      cout << "The File Was Unable To Open For An Unknown Reason." << endl;
      cout << "				  Program Terminated" << endl;
      system("PAUSE");
      return 0;
      }
      thanks

      Comment

      • compman9902
        New Member
        • Mar 2007
        • 105

        #4
        Originally posted by compman9902
        That was very......helpf ul
        could I please have some code with that?
        just, use this example:
        Code:
        ofstream filesave ("C:\\my text file.txt");
        if (filesave.is_open())
        {
        filesave << testToBeAdded;
        filesave.close();
        }
        else
        {
        cout << "The File Was Unable To Open For An Unknown Reason." << endl;
        cout << "				  Program Terminated" << endl;
        system("PAUSE");
        return 0;
        }
        thanks
        Well, I just figureed out how to append text files. But, now the question is a little bit harder: How do I add the line of text that I want to the end of the file? Every time I run the program, it just rewirtes the first line. How do I fix this?



        --Parker Woods
        --MonsterHackz DEV Team

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by compman9902
          Well, I just figureed out how to append text files. But, now the question is a little bit harder: How do I add the line of text that I want to the end of the file? Every time I run the program, it just rewirtes the first line. How do I fix this?



          --Parker Woods
          --MonsterHackz DEV Team
          You could use the fseek() function. Look at this:

          Comment

          • bluesteel
            New Member
            • Mar 2007
            • 84

            #6
            Originally posted by compman9902
            Well, I just figureed out how to append text files. But, now the question is a little bit harder: How do I add the line of text that I want to the end of the file? Every time I run the program, it just rewirtes the first line. How do I fix this?



            --Parker Woods
            --MonsterHackz DEV Team
            Well, i have done it, not in a very practical way but anyway.
            I used fopen("Filename ","mode"); where mode was the mode it would be opened, i used "r+" because it let you open without erasing and you could also edit.
            To get to the end, i used some functions, although i know it is quite impractical but at least it does work.

            Code:
            FILE *myfile;
            if((myfile=fopen("File.txt","r+"))==NULL)printf("File could not be opened");
            else {
            while(!feof(myfile)){//while it is not the end
            fgetc(myfile);
            }
            }
            Then you will get to the end.
            You can also write '\n' so that you write in the following line.
            For that i use fwrite().
            The syntax is fwrite("Text",i nt length,int times,file);
            you would use it in this way:
            fwrite("Hello World",11,1,myf ile);
            The second parameter is not exactly the size, it's the length in bytes, but since a character is a byte, then you can say it's the size in number of words.
            If you want to write a value stored in an array write & before the name of the string

            Comment

            • compman9902
              New Member
              • Mar 2007
              • 105

              #7
              Originally posted by bluesteel
              Well, i have done it, not in a very practical way but anyway.
              I used fopen("Filename ","mode"); where mode was the mode it would be opened, i used "r+" because it let you open without erasing and you could also edit.
              To get to the end, i used some functions, although i know it is quite impractical but at least it does work.

              Code:
              FILE *myfile;
              if((myfile=fopen("File.txt","r+"))==NULL)printf("File could not be opened");
              else {
              while(!feof(myfile)){//while it is not the end
              fgetc(myfile);
              }
              }
              Then you will get to the end.
              You can also write '\n' so that you write in the following line.
              For that i use fwrite().
              The syntax is fwrite("Text",i nt length,int times,file);
              you would use it in this way:
              fwrite("Hello World",11,1,myf ile);
              The second parameter is not exactly the size, it's the length in bytes, but since a character is a byte, then you can say it's the size in number of words.
              If you want to write a value stored in an array write & before the name of the string
              These are all great solutions, but I do not want to use fopen() or fseek(). I only can use fstream. Sorry, and thank you for you help.

              Comment

              Working...