Thread: help with renumbering entries in record file

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Windsor, CA
    Posts
    5

    Question help with renumbering entries in record file

    -----First example-----

    1,firstguy,hispassword
    2,nextguy, hispassword

    ------------------



    i want to change numbers here and add a 10000 to front of them so it would read

    100001,firstguy,hispassword
    100002,nextguy, hispassword




    -------- 2nd example -------
    1, A_name, 4 , more data, etc
    ----------------------------------------------------------------

    i want to change first and second numbers here and add a 10000 to front of them so it would read



    100001, A_name, 100004, more data, etc


    Note:


    this is going to be on a large scale with hundreds of records to change to that kind of thing
    here is my lame code that doesn't work. take a look at it if you can solve this problem that would be really cool.
    thanks for looking


    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        ifstream infile;
        ofstream outfile;
        string mystring;
        infile.open("in.txt");
        outfile.open("out.txt");
        
        
    while(infile)
    {
        infile.getline(mystring);
        outfile.write("10000");
        outfile.write(mystring);
            
    }
        outfile.close();
        infile.close();
    return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can read a line like this (or read the whole thing and use stringstreams to parse it -- see the FAQ):
    Code:
    while(file >> number >> comma >> name >> comma >> password) {
    }
    Also see this tutorial. http://www.cprogramming.com/tutorial/lesson10.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Windsor, CA
    Posts
    5
    thanks! ill try that

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Actually, I didn't realize that your problem was so simple. You could get away with
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        ifstream infile;
        ofstream outfile;
        string mystring;
        infile.open("in.txt");
        outfile.open("out.txt");
        
        
    while(!infile.eof())
    {
        infile.getline(mystring);
        outfile.write("10000");
        outfile.write(mystring);
            
    }
        outfile.close();
        infile.close();
    return 0;
    }
    You should also use .is_open() to see if the files were opened correctly.

    [edit] Never mind, you were using the wrong functions and I didn't notice . . . maybe something more like
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        ifstream infile("in.txt");
        ofstream outfile("out.txt");
        string mystring;
        
        while(!infile.eof()) {
            getline(infile, mystring);
            outfile << "10000" << mystring << endl;
        }
        
        outfile.close();
        infile.close();
        return 0;
    }
    [/edit]
    Last edited by dwks; 07-13-2007 at 11:32 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if getline failed - the loop will exit only on the next iteration...
    Code:
    while(getline(infile, mystring))
    {
            outfile << "10000" << mystring << endl;
    }
    maybe better
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> while(!infile.eof())
    This is a bad suggestion, as it can cause the last line to be processed twice. The OP's version was better, vart's version is best if you use getline.

    >> while(file >> number >> comma >> name >> comma >> password) {
    However, I like this suggestion best overall and better than using getline. Write the data back out with
    Code:
    outfile << number+100000 << ',' << name << ',' << password;
    This is better because it scales to the second problem more easily.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't know what I was thinking with the while(!eof) . . . I had stuck that in, meaning to change it, and forgot to do so. That's what happens when you're in a hurry.

    >> while(file >> number >> comma >> name >> comma >> password) {
    However, I like this suggestion best overall and better than using getline.
    It's difficult to handle errors with that sort of setup, however.

    Perhaps the best solution of all would be a hybrid, something like this:
    Code:
    #include <sstream>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        ifstream infile("in.txt");
        ofstream outfile("out.txt");
        string mystring;
        string name, password;
        int number;
        char comma;
        
        while(getline(infile, mystring)) {
            istringstream ss(mystring);
            ss >> number >> comma >> name >> comma >> password;
            outfile << number+100000 << ',' << name << ',' << password;
        }
        
        outfile.close();
        infile.close();
        return 0;
    }
    It needs some error handling, of course. Like checking that the characters read from ss into comma really are commas, and seeing if the files could be opened.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> It's difficult to handle errors with that sort of setup, however.
    Your stringstream solution doesn't really handle the errors much better. You'd want to check the return value of the read from the stringstream.

    I don't think the stringstream is necessary, though. It's benefit is that if one row is incorrectly formatted it will continue to process other rows. If you want to handle invalid rows without the string stream you just have to separate the read from the while control and then clear(), ignore() and continue if it fails. If you're sure the file is properly formatted that isn't even necessary.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Location
    Windsor, CA
    Posts
    5
    Code:
    #include <sstream>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        ifstream infile("in.txt");
        ofstream outfile("out.txt");
        string mystring;
        string name, password;
        int number;
        char comma;
        
        while(getline(infile, mystring)) {
            istringstream ss(mystring);
            ss >> number >> comma >> name >> comma >> password;
            outfile << number+100000 << ',' << name << ',' << password << endl;
        }
        
        outfile.close();
        infile.close();
        return 0;
    }

    ok so i added an endl to the outfile thing but when i run the program i get
    "100001,firstguy,hispassword100002,nextguy,hispass word"
    as my output so why would it ignoring the endl and not skip a line in the output file?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    D'oh, it's reading the name as "firstguy,hispassword", leaving nothing for the password. You can't just use the istringstream >> operator to get the name and the password, or if you do, you can't separate them out.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Location
    Windsor, CA
    Posts
    5
    well i wrote it wrong before the way it appears in the file is like

    1 , username , password
    2 , username , password

    and

    1 , username , 4 , data , moredata <---its seperated by a space then a comma then another space " , "

    thats what you were wondering about right cause in the file its not set up like
    1,username,password
    but like
    1 , username , password

    ok well hope that helps and thanks again for helping me with this program!

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Right, that makes things easier . . . this works for me:
    Code:
    #include <sstream>
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        ifstream infile("in.txt");
        ofstream outfile("out.txt");
        string mystring;
        char name[80], password[80];
        int number;
        char comma1, comma2;
        
        while(getline(infile, mystring)) {
            istringstream ss(mystring);
            if(ss >> number >> comma1 >> name >> comma2 >> password
                && comma1 == ',' && comma2 == ',') {
                
                outfile << number + 100000 << " , " << name << " , " << password
                    << endl;
            }
            else {
                clog << "Error processing line: " << mystring << endl;
            }
        }
        
        outfile.close();
        infile.close();
        return 0;
    }
    The output is
    Code:
    $ cat in.txt
    1 , john , pass
    2 , sam , word
    
    $ ./test2
    
    $ cat out.txt
    100001 , john , pass
    100002 , sam , word
    
    $
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jul 2007
    Location
    Windsor, CA
    Posts
    5
    thanks again for the help! ok well i can modify your code to fit the extra collums that ill need for the 2nd example and i think that im good now oh what does

    char name[80];

    exatcly mean btw?

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oops . . . it should be a string. Those are old-style C-strings. You shouldn't use them. And I should proof-read my code more carefully before posting it . . .

    http://www.cprogramming.com/tutorial/lesson9.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Snej-coder View Post
    oh what does

    char name[80];

    exatcly mean btw?
    reserve space for an array of 80 characters and call it name.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM