How to modify a file using C++ file objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Venkat

    How to modify a file using C++ file objects

    Greetings to All,

    I have the following file and i need to modify it with the contents of an
    array

    File1.txt

    Name Location Points Grade
    Venkat,Newyork, 100,A
    Jack,LA,12,C
    Heather,Las Vegas,190,B
    Jay,Sanjose,199 ,A
    John,Austin,1,D

    int NewPoints = {23,1,100,181,1 00}


    Now i need to write a program using file objects of C++(ifstream, ofstream)
    such that it should open the file File1.txt and should modify the values
    under Points section with that present in NewPoints.


    It is urgent please let me know how to go about it.


    Thanks and Regards,
    Venkat





  • Nirmalya Ghosh Chowdhury

    #2
    Re: How to modify a file using C++ file objects

    Sounds like homework.


    Venkat wrote:[color=blue]
    > Greetings to All,
    >
    > I have the following file and i need to modify it with the contents of an
    > array
    >
    > File1.txt
    >
    > Name Location Points Grade
    > Venkat,Newyork, 100,A
    > Jack,LA,12,C
    > Heather,Las Vegas,190,B
    > Jay,Sanjose,199 ,A
    > John,Austin,1,D
    >
    > int NewPoints = {23,1,100,181,1 00}
    >
    >
    > Now i need to write a program using file objects of C++(ifstream, ofstream)
    > such that it should open the file File1.txt and should modify the values
    > under Points section with that present in NewPoints.
    >
    >
    > It is urgent please let me know how to go about it.
    >
    >
    > Thanks and Regards,
    > Venkat
    >
    >
    >
    >
    >[/color]

    Comment

    • osmium

      #3
      Re: How to modify a file using C++ file objects

      Venkat writes:
      [color=blue]
      > I have the following file and i need to modify it with the contents of an
      > array
      >
      > File1.txt
      >
      > Name Location Points Grade
      > Venkat,Newyork, 100,A
      > Jack,LA,12,C
      > Heather,Las Vegas,190,B
      > Jay,Sanjose,199 ,A
      > John,Austin,1,D
      >
      > int NewPoints = {23,1,100,181,1 00}
      >
      >
      > Now i need to write a program using file objects of C++(ifstream,[/color]
      ofstream)[color=blue]
      > such that it should open the file File1.txt and should modify the values
      > under Points section with that present in NewPoints.[/color]

      First of all, you will have to create a new file and copy most of the old
      stuff to the new file. Then delete the old file and rename the new file.
      Done.

      The reason for this is that the hard drive is used to emulate thousands of
      tape drives, each file is really treated as a tiny tape unit (with a
      potentially huge capacity!). And as you know, you can not add 'stuff' to an
      audio tape or any other kind of tape. Note that the least record in your
      exercise goes from 1 to 100. (You can't remove stuff either, but the adding
      argument is probably more persuasive.)

      Modify the points file as the new file is being created.

      I doubt if the latest whiz-bang version of the C++ language provides all you
      need, but there are some old, rusty tools in <cstdio> which will do the file
      renaming bit.


      Comment

      • Venkat

        #4
        Re: How to modify a file using C++ file objects

        "Nirmalya Ghosh Chowdhury" <ngcXXXXX@ngc.c om> wrote in message
        news:3FFBFD2B.1 020007@ngc.com. ..[color=blue]
        > Sounds like homework.[/color]

        Hi Nirmalya,

        Thanks for a hilarious comment. Really i am getting laugh on your comment
        ha ha ha........
        I want to make my question simple so that it would be easy for others to
        understand and answer it.

        My application involves writing a dll which when lanched calls the
        file1.txt in question it is a CSV(Comma Seperated Value) file and replaces
        its contents dynammically.

        I knew the procedure of reading and writing to a file using ifstream and
        ofstream objects but was wondering is there any wayout to edit the file at a
        particular place.

        To my understanding this newsgroup is meant to share the knowledge and i
        am no naive to this group, i had been actively contributing to this group
        from the past 2 years.

        But to be frank comments like this would refrain people from using this
        group.


        regards,
        Venkat




        Comment

        • Chris \( Val \)

          #5
          Re: How to modify a file using C++ file objects


          "Venkat" <venkat_kp@yaho o.com> wrote in message news:1073477752 .960388@sj-nntpcache-5...
          | Greetings to All,
          |
          | I have the following file and i need to modify it with the contents of an
          | array
          |
          | File1.txt
          |
          | Name Location Points Grade
          | Venkat,Newyork, 100,A
          | Jack,LA,12,C
          | Heather,Las Vegas,190,B
          | Jay,Sanjose,199 ,A
          | John,Austin,1,D
          |
          | int NewPoints = {23,1,100,181,1 00}
          |
          |
          | Now i need to write a program using file objects of C++(ifstream, ofstream)
          | such that it should open the file File1.txt and should modify the values
          | under Points section with that present in NewPoints.
          |
          |
          | It is urgent please let me know how to go about it.

          Try this for starters:

          # include <iostream>
          # include <fstream>
          # include <ostream>
          # include <string>
          # include <sstream>

          template<class T>
          inline bool UpdateFile( const char* Old, const char* New,
          const int& Col, const T& Array )
          {
          std::ifstream InFile( Old );
          std::ofstream OutFile( New );

          if( !InFile || !OutFile )
          return false;

          std::string::si ze_type ArrayIndex( 0 );
          std::string::si ze_type Column( Col );
          std::string::si ze_type FieldPosition( 0 );

          std::string Line, Buffer;
          while( std::getline( InFile, Line ) )
          {
          std::stringstre am SS( Line );
          while( std::getline( SS, Buffer, ',' ) )
          {
          if( Column == FieldPosition )
          OutFile << Array[ ArrayIndex ] << " ";
          else
          OutFile << Buffer << " ";
          ++FieldPosition ;
          }

          OutFile << std::endl;

          FieldPosition = 0;
          ++ArrayIndex;
          }

          return true;
          }

          int main()
          {
          int NewPoints[] = { 23, 1, 100, 181, 100 };

          if( UpdateFile( "MyFile.txt ", "NewFile.tx t", 2, NewPoints ) )
          std::cout << "Done " << std::endl;
          else
          std::cout << "Some error occured " << std::endl;

          return 0;
          }

          This will not remove the original, but should be able to do
          that part :-). Just use 'std::remove()' and 'std::rename()'
          in <cstdio>.

          Cheers.
          Chris Val


          Comment

          • osmium

            #6
            Re: How to modify a file using C++ file objects

            Chris ( Val ) writes:

            [Scan way... down][color=blue]
            > "Venkat" <venkat_kp@yaho o.com> wrote in message[/color]
            news:1073477752 .960388@sj-nntpcache-5...[color=blue]
            > | Greetings to All,
            > |
            > | I have the following file and i need to modify it with the contents of[/color]
            an[color=blue]
            > | array
            > |
            > | File1.txt
            > |
            > | Name Location Points Grade
            > | Venkat,Newyork, 100,A
            > | Jack,LA,12,C
            > | Heather,Las Vegas,190,B
            > | Jay,Sanjose,199 ,A
            > | John,Austin,1,D
            > |
            > | int NewPoints = {23,1,100,181,1 00}
            > |
            > |
            > | Now i need to write a program using file objects of C++(ifstream,[/color]
            ofstream)[color=blue]
            > | such that it should open the file File1.txt and should modify the values
            > | under Points section with that present in NewPoints.
            > |
            > |
            > | It is urgent please let me know how to go about it.
            >
            > Try this for starters:
            >
            > # include <iostream>
            > # include <fstream>
            > # include <ostream>
            > # include <string>
            > # include <sstream>
            >
            > template<class T>
            > inline bool UpdateFile( const char* Old, const char* New,
            > const int& Col, const T& Array )
            > {
            > std::ifstream InFile( Old );
            > std::ofstream OutFile( New );
            >
            > if( !InFile || !OutFile )
            > return false;
            >
            > std::string::si ze_type ArrayIndex( 0 );
            > std::string::si ze_type Column( Col );
            > std::string::si ze_type FieldPosition( 0 );
            >
            > std::string Line, Buffer;
            > while( std::getline( InFile, Line ) )
            > {
            > std::stringstre am SS( Line );
            > while( std::getline( SS, Buffer, ',' ) )
            > {
            > if( Column == FieldPosition )
            > OutFile << Array[ ArrayIndex ] << " ";
            > else
            > OutFile << Buffer << " ";
            > ++FieldPosition ;
            > }
            >
            > OutFile << std::endl;
            >
            > FieldPosition = 0;
            > ++ArrayIndex;
            > }
            >
            > return true;
            > }
            >
            > int main()
            > {
            > int NewPoints[] = { 23, 1, 100, 181, 100 };
            >
            > if( UpdateFile( "MyFile.txt ", "NewFile.tx t", 2, NewPoints ) )
            > std::cout << "Done " << std::endl;
            > else
            > std::cout << "Some error occured " << std::endl;
            >
            > return 0;
            > }
            >
            > This will not remove the original, but should be able to do
            > that part :-). Just use 'std::remove()' and 'std::rename()'
            > in <cstdio>.
            >
            > Cheers.
            > Chris Val[/color]

            Couldn't you detect from the wording that that was a student assignment????
            Do you think you are really helping him by posting actual code??? Did you
            ever hear the parable about the man and the fish?

            -- Osmium


            Comment

            • Karl Heinz Buchegger

              #7
              Re: How to modify a file using C++ file objects

              Venkat wrote:[color=blue]
              >
              > To my understanding this newsgroup is meant to share the knowledge and i
              > am no naive to this group, i had been actively contributing to this group
              > from the past 2 years.[/color]

              That's hard to believe given that your question covers an
              extremely basic (and simple) job.


              --
              Karl Heinz Buchegger
              kbuchegg@gascad .at

              Comment

              • dwrayment

                #8
                Re: How to modify a file using C++ file objects

                look up fseek in your c++ help file


                "Venkat" <venkat_kp@yaho o.com> wrote in message
                news:1073477752 .960388@sj-nntpcache-5...[color=blue]
                > Greetings to All,
                >
                > I have the following file and i need to modify it with the contents of an
                > array
                >
                > File1.txt
                >
                > Name Location Points Grade
                > Venkat,Newyork, 100,A
                > Jack,LA,12,C
                > Heather,Las Vegas,190,B
                > Jay,Sanjose,199 ,A
                > John,Austin,1,D
                >
                > int NewPoints = {23,1,100,181,1 00}
                >
                >
                > Now i need to write a program using file objects of C++(ifstream,[/color]
                ofstream)[color=blue]
                > such that it should open the file File1.txt and should modify the values
                > under Points section with that present in NewPoints.
                >
                >
                > It is urgent please let me know how to go about it.
                >
                >
                > Thanks and Regards,
                > Venkat
                >
                >
                >
                >
                >[/color]


                Comment

                • jeffc

                  #9
                  Re: How to modify a file using C++ file objects


                  "Venkat" <venkat_kp@yaho o.com> wrote in message
                  news:1073480340 .695754@sj-nntpcache-3...[color=blue]
                  >
                  > To my understanding this newsgroup is meant to share the knowledge and i
                  > am no naive to this group, i had been actively contributing to this group
                  > from the past 2 years.[/color]

                  Your name looks new to me.
                  [color=blue]
                  > But to be frank comments like this would refrain people from using this
                  > group.[/color]

                  If telling people we won't do their homework for them makes them refrain
                  from using the group, then so be it.


                  Comment

                  • Karl Heinz Buchegger

                    #10
                    Re: How to modify a file using C++ file objects

                    dwrayment wrote:[color=blue]
                    >
                    > text mode, binary mode same s**t you can open whatever mode you like and to
                    > the same job.[/color]

                    One more time: Please don't top post. Put your reply underneath the text
                    you are replying to and snip from the reply what you don't need any longer.

                    Not if you are working with a text file which doesn't have a constant line
                    length. The OP's file seems to be of that sort.
                    [color=blue]
                    >
                    > and fseek will do the job.
                    >[/color]

                    fseek could do the job, but it's much more complicated then simply
                    writing a new file.

                    The reason:
                    look at the numbers:

                    (Although the poster didn't mention it explicitely, it is still safe to
                    assume that this is a text file, since we have seen the very same homework
                    assignment a number of times in the past with the very same input file).
                    [color=blue][color=green][color=darkred]
                    > > >
                    > > > Name Location Points Grade
                    > > > Venkat,Newyork, 100,A
                    > > > Jack,LA,12,C
                    > > > Heather,Las Vegas,190,B
                    > > > Jay,Sanjose,199 ,A
                    > > > John,Austin,1,D
                    > > >
                    > > > int NewPoints = {23,1,100,181,1 00}
                    > > >[/color][/color][/color]


                    Replace (in your mind) the '100' in the first record with 23. You can't do
                    it without moving the rest of the line (and the rest of the file) also.
                    You will agree that fseek is't really that much usefull in doing this.
                    Same for the last line: replace '1' with '100'. In order to do this the line
                    (and the rest of the file) has to be enlarged. Again: fseek isn't much of use in this.

                    While it theoretically is possible to do all the modifications on the original
                    text file:

                    remember the start of a line
                    reading a line into memory
                    do the modification in memory
                    seeke back to the start of the line at the file
                    determining if the file needs to be enlarger or shrinked
                    if so do the enlarge or shrink operation (by reading the rest
                    of the file into a buffer and rewriting it to the file such that
                    the gap is shortened or enlarged)
                    write the modified line back to the file

                    it turns out that the enlarge or shrink operation costs that much CPU and/or
                    I/O time and/or memory for the buffer, that it isn't worth the attempt.

                    --
                    Karl Heinz Buchegger
                    kbuchegg@gascad .at

                    Comment

                    • Chris Theis

                      #11
                      Re: How to modify a file using C++ file objects


                      "dwrayment" <dwrayment@roge rs.com> wrote in message
                      news:7QWKb.2059 7$AAe1.5545@new s01.bloor.is.ne t.cable.rogers. com...[color=blue]
                      > look up fseek in your c++ help file[/color]

                      Please don“t top post. Furthermore fseek will not be of much help in this
                      case as the OP is not dealing with a binary file where he can specify
                      exactly at which location (meaning which byte) the contents is found which
                      should be replaced.

                      Regards
                      Chris


                      Comment

                      • dwrayment

                        #12
                        Re: How to modify a file using C++ file objects

                        text mode, binary mode same s**t you can open whatever mode you like and to
                        the same job.

                        and fseek will do the job.

                        "dwrayment" <dwrayment@roge rs.com> wrote in message
                        news:7QWKb.2059 7$AAe1.5545@new s01.bloor.is.ne t.cable.rogers. com...[color=blue]
                        > look up fseek in your c++ help file
                        >
                        >
                        > "Venkat" <venkat_kp@yaho o.com> wrote in message
                        > news:1073477752 .960388@sj-nntpcache-5...[color=green]
                        > > Greetings to All,
                        > >
                        > > I have the following file and i need to modify it with the contents of[/color][/color]
                        an[color=blue][color=green]
                        > > array
                        > >
                        > > File1.txt
                        > >
                        > > Name Location Points Grade
                        > > Venkat,Newyork, 100,A
                        > > Jack,LA,12,C
                        > > Heather,Las Vegas,190,B
                        > > Jay,Sanjose,199 ,A
                        > > John,Austin,1,D
                        > >
                        > > int NewPoints = {23,1,100,181,1 00}
                        > >
                        > >
                        > > Now i need to write a program using file objects of C++(ifstream,[/color]
                        > ofstream)[color=green]
                        > > such that it should open the file File1.txt and should modify the values
                        > > under Points section with that present in NewPoints.
                        > >
                        > >
                        > > It is urgent please let me know how to go about it.
                        > >
                        > >
                        > > Thanks and Regards,
                        > > Venkat
                        > >
                        > >
                        > >
                        > >
                        > >[/color]
                        >
                        >[/color]


                        Comment

                        • Chris \( Val \)

                          #13
                          Re: How to modify a file using C++ file objects


                          "osmium" <r124c4u102@com cast.net> wrote in message
                          news:bthcod$6s1 m3$1@ID-179017.news.uni-berlin.de...
                          | Chris ( Val ) writes:

                          [snip]

                          | Couldn't you detect from the wording that that was a student assignment????
                          | Do you think you are really helping him by posting actual code??? Did you
                          | ever hear the parable about the man and the fish?

                          My apologies to the group if it was an assignment, but at the
                          time of posting, I didn't realise it, otherwise, I would not
                          have provided it.

                          Cheers.
                          Chris Val


                          Comment

                          Working...