Reading File, Searching, Output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sharkolomew
    New Member
    • Apr 2007
    • 5

    Reading File, Searching, Output

    Hi all. I have a problem

    I want my program to do the following.

    Create a file of student ID #s.
    Then, Read the file.
    Search for the line of student ID # say 10002.
    At that line, input test scores received from input earlier in program.

    Initial file should look like:

    10001
    10002
    10003
    10004

    After output:

    10001
    10002 65 45 35 34 //or "65,45,35,3 4" does not matter if there are spaces, commas, etc.
    10003
    10004

    Now, I can create the student ID file fine, But I am not sure how to read and output exactly on that line. I am using fstream, but it seems to erase the whole file when it is finished.

    Any help is appreciated. Thank you.
  • Sharkolomew
    New Member
    • Apr 2007
    • 5

    #2
    Any ideas, please?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Sharkolomew
      Hi all. I have a problem

      I want my program to do the following.

      Create a file of student ID #s.
      Then, Read the file.
      Search for the line of student ID # say 10002.
      At that line, input test scores received from input earlier in program.

      Initial file should look like:

      10001
      10002
      10003
      10004

      After output:

      10001
      10002 65 45 35 34 //or "65,45,35,3 4" does not matter if there are spaces, commas, etc.
      10003
      10004

      Now, I can create the student ID file fine, But I am not sure how to read and output exactly on that line. I am using fstream, but it seems to erase the whole file when it is finished.

      Any help is appreciated. Thank you.
      I hate to ask for code but maybe if you post the code that you think is wrong ...

      Comment

      • Sharkolomew
        New Member
        • Apr 2007
        • 5

        #4
        sure,

        this code represents modifying the student data after the data file contains lines for each student number.

        Code:
        cout<<"Enter student ID # to modify: ";
        cin>>studentID;
        cout<<"Enter all student scores: ";
        cin>>scores;  //this could be either "50 50 50" or "50,50,50" if its easier to read in
        fstream f;
        f.open("data.txt");
        while(f>>input)
        { 
           if (input==studentID)
              {
                  cout<<"found";                 //shows that it is finding the student ID
                   f<<studentID<<" "<<scores<<end;    //i think the problem is here
              }
        }

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by Sharkolomew
          sure,

          this code represents modifying the student data after the data file contains lines for each student number.

          Code:
          cout<<"Enter student ID # to modify: ";
          cin>>studentID;
          cout<<"Enter all student scores: ";
          cin>>scores;  //this could be either "50 50 50" or "50,50,50" if its easier to read in
          fstream f;
          f.open("data.txt");
          while(f>>input)
          { 
             if (input==studentID)
                {
                    cout<<"found";                 //shows that it is finding the student ID
                     f<<studentID<<" "<<scores<<end;    //i think the problem is here
                }
          }
          I think you need to open the file in append mode. That way you can add on to it without erasing the whole file. Try this:
          Code:
          f.open("data.txt", ios_base::app);
          but I'm not sure if that's the way to set it to append mode.
          Also, you might want to try using the fseek function. That way you can go to a position in a file.

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by ilikepython
            Code:
            f.open("data.txt", ios_base::app);
            but I'm not sure if that's the way to set it to append mode.
            .
            Yes,that's the way for sure!!!

            Nice ilikepython.

            Savage

            Comment

            • Sharkolomew
              New Member
              • Apr 2007
              • 5

              #7
              Okay, my code is no longer disappearing, but it is not putting out the scores still.

              How does the fseek function work?

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by Sharkolomew
                Okay, my code is no longer disappearing, but it is not putting out the scores still.

                How does the fseek function work?
                first here is declaration: int fseek(FILE *stream,offset, whence);
                fseek function works this way:

                It sets a pointer assigned to the FILE stream on location in file which depands on offset and whence.Offset represent differance in bytes beetwen whence and new location.Whence represent a file pointer location and have 3 possible values:

                SEEK_SET seeks from begining of the file
                SEEK_CUR seeks from current location and
                SEEK_END seeks from EOF.

                Savage

                Comment

                • Sharkolomew
                  New Member
                  • Apr 2007
                  • 5

                  #9
                  Well turns out append mode is not what I am looking for. I need to insert or replace text on the current line, not at the end.

                  To get to the current line, I use while(f>>input) //input is an int\

                  Once it finds the line, I want to add to that current line and keep the rest of the file intact.

                  Fseek looks like it may work, but I cant find a relevant example in replacing text when I googled it.

                  Comment

                  Working...