Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Question about c++ file i/o

  1. Mar 8, 2008 #1
    i have a txt file full of numbers and i just want to take the first 5 digits, save it has one number, shift over 1 digit, take another 5 digits, save it, and so on until the end of the file

    here is the code
    Code (Text):

        fstream Prime;
        Prime.open("digitsofe.txt");
       
        if (!Prime.is_open()) { cout << "unable to open"; }
        else if (Prime.is_open()) {
            cout << "File opened successfully\n";
            Prime.getline(str2,6);
            cout << str2 << endl;
            Prime.seekg(1);
            Prime.getline(str2,6);
            cout << str2;
           
            Prime.close();
    }
     
    right now i'm just trying to print the first 5 digits and then the next 5 digits but the second number won't print. what am i doing wrong?

    I tried using the get function but using atoi kept giving errors.
     
  2. jcsd
  3. Mar 9, 2008 #2
    You are going right, so now, check the error flags in Prime.

    cout << str2 << endl;
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Question about c++ file i/o
  1. C++ Simple File I/O (Replies: 12)

  2. C file I/O (Replies: 3)

  3. FILE I/O in an IDE (Replies: 1)

  4. C++ file i/o (Replies: 7)

Loading...