Thread: string i/o with files

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    66

    string i/o with files

    I'm trying to figure out how to work with i/o from files.

    Code:
        char temp[100];
        ifstream accfile;
        accfile.open ("something.txt");
        accfile >> temp;
        cout << temp;
    What I'd like to do is to read a string from the file until a certain character is found and stop without reading it (or drop it) or a newline is found. I want to keep reading until the newline appears and then start reading at the next line.

    cin.get was mentioned in the lecture, but I can't find any good examples through google of how to use it with a file.

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I've been trying to do
    Code:
        cin.getline(accfile, 100, ';') >> temp;
        cout << temp;
    I've been getting these errors in Visual Studio:
    In function `int main()':|
    error: invalid conversion from `void*' to `char*'|
    error: initializing argument 1 of `std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]'|
    ||=== Build finished: 2 errors, 0 warnings ===|
    Last edited by Furious5k; 12-19-2008 at 07:33 AM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    std::string line;
    std::getline(std::cin, line, ';');
    std::cout << line;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Thank you, that worked. Could somebody explain why the "std::" in front is required?



    Code:
    for (int i = 1; i < 5; i++)
    {
        std::string line;
        std::getline(accfile, line, ';');
        std::cout << line;
    }
    As you can see I have 4 seperate pieces of information in the file, each terminating with ";". How would I get it to repeat the loop on the next line in the file, until there is nothing left to read in? Would I use !cin to see that the file has ended?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Everything in the std namespace has always needed std:: in front of it. cin, cout, getline, etc. You can put using namespace std; somewhere in your code if you don't like typing.

    Obviously you can't check std::cin if you want to know if accfile is out of data. You could check !accfile, but accfile won't go "bad" until you try to read something that isn't there, so you can't check until after you do a read.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I have "using namespace std;" at the top which is why I was confused that it worked after I added "std::" in front.


    Anyhow, I fixed the problem of reading all the data with nested getline, a solution I found online. However, I'm having a problem trying to store this data in a struct array. (I don't currently have Internet access on the same PC I'm writing the code on, so I can't provide the error I was receiving. So I'm hoping someone can tell me how to correctly store this data, instead of outputting it screen (which works exactly how I want it to)).

    "Books" is a struct.
    Code:
    if ( accfile )
        {
            string temp;
            stringstream subtemp;
            while ( getline (accfile, line) )
            {
                subtemp<< line;
                while ( getline (subtemp, temp, ';') )
                {
                    //cout << temp << endl;
    
                   void getToken(char Mystring[])
                    {
                        char *pch;
                        pch = strtok(Mystring);
    
                        while (pch !=0)
                        {
                            Books << pch;
                            pch = strtok(Null);
                        }
                    }
                }
                subtemp.clear();
            }
    
        }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Furious5k
    I have "using namespace std;" at the top which is why I was confused that it worked after I added "std::" in front.
    That's weird. I would ask that you provide the smallest and simplest code that demonstrates the error, but if the problem is already fixed then nevermind.

    Quote Originally Posted by Furious5k
    However, I'm having a problem trying to store this data in a struct array.
    It looks like you are trying to define the getToken() function in the middle of another function. Move the function definition outside of the function and call getToken() instead.

    Quote Originally Posted by Furious5k
    I don't currently have Internet access on the same PC I'm writing the code on, so I can't provide the error I was receiving.
    I suggest that you install a C++ compiler on the computer that you use to write the code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. Searching txt files (another file i/o qn)
    By Catif in forum C++ Programming
    Replies: 9
    Last Post: 05-13-2002, 04:14 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM