string i/o with files

This is a discussion on string i/o with files within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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
    Posts
    22,915
    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,185
    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
    22,159
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:30 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04: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, 05:14 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2001, 12:33 AM

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