file I/O??

This is a discussion on file I/O?? within the C++ Programming forums, part of the General Programming Boards category; Hey, I was wondering how do you actually open and use the information already in a file once you have ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    file I/O??

    Hey,
    I was wondering how do you actually open and use the information already in a file once you have brought it into the program.
    I need to use the information in it and then change it but I cannot figure out how to do this?
    Thanks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Open it up, read it into a string buffer, edit to your hearts content, write it back to the file.

    Code:
    int main() {
        std::ifstream inFile;
        std::ofstream outFile;
        std::string fileBuffer = "", 
                    lineBuffer = "";
    
        inFile.open("myfile.txt");
    
        while(getline(inFile, lineBuffer, '\n'))
            fileBuffer += lineBuffer + '\n';   // Don't forget to add the delimeter
           
        inFile.close();
         
        /* Using an assortment of std::string::find(), std::string::substr(), 
           std::string::replace(), etc... you edit your string here */
           
        outFile.open("myfile.txt", std::ios::trunc);
        
        outFile << fileBuffer;
        
        outFile.close();
        
        return 0;
    }
    Last edited by SlyMaelstrom; 05-02-2006 at 07:22 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Thankyou so much for that but what is a string buffer?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Added code to an edit.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Ok so say I had a file that had "hello" written in it and I needed to turn "hello" into ASCII values and then show these in the output (only the numbers not the original file material) how would I do that?
    I have already done everything to get the file and open it I just can't figure out how to do the rest.

    I actually need to add all the values together and output the result
    Last edited by rachael033; 05-02-2006 at 07:19 AM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    std::string word = "hello";
    int asciiWord[word.size()];
    
    for(int i = 0; i < word.size(); i++)
       asciiWord[i] = (int)word[i];
    
    for(int i = 0; i < word.size(); i++)
       std::cout << asciiWord[i] << ' ';
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    So if "hello" was in a file called file1.txt which is the part where the information in the file is used?

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by SlyMaelstrom
    Code:
    std::string word = "hello";
    int asciiWord[word.size()];
    
    for(int i = 0; i < word.size(); i++)
       asciiWord[i] = (int)word[i];
    
    for(int i = 0; i < word.size(); i++)
       std::cout << asciiWord[i] << ' ';
    I think your milk has spoiled.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    Ok I don't understand it
    Thanks anyway

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by hk_mp5kpdw
    I think your milk has spoiled.
    I actually thought it wouldn't compile with the potentially variable integer, but it did fine without warning. I supposed there may be some compilers that would have a problem with it.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> I have already done everything to get the file and open it I just can't figure out how to do the rest.

    Show us that code, whatever you already have. It will make it easier to understand what the next step is that you are having trouble with.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    using namespace std;

    int length_of_syracuse(int n);

    bool isPrime(int a);

    int largest_prime_factor(int a);

    int main()
    {
    int sumofints;

    ifstream inFile;
    char filename[20];

    cout << "Enter the name of the input file: "<<flush;
    cin >> filename;
    inFile.open(filename);



    So the file is open but i can't figure out how to use the information in it.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    I explained what to do in my first post. Look at the code.
    Sent from my iPadŽ

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Just like you do:
    Code:
    cin >> filename;
    ...to read data that the user types in the console into a variable, you can do:
    Code:
    int var;
    inFile >> var;
    ...to read from your open file into a variable.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21