getline function is being weird

This is a discussion on getline function is being weird within the C++ Programming forums, part of the General Programming Boards category; Code: char dataIn[80]; char m[30] = "222222" char cS[30] = "CCCC" while (!ins.eof()){ ins.getline(dataIn, '\n'); outs << m + cS ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    16

    getline function is being weird

    Code:
    char dataIn[80];
    char m[30] = "222222"
    char cS[30] = "CCCC"
    	while (!ins.eof()){
    
    	ins.getline(dataIn, '\n');
    	outs << m + cS + dataIn << endl;
    
    	}

    the problem is it only inserts the dataIn once, i get the m, and cS every line, but the dataIn only outputs once.

    the output looks something like this

    Code:
        
    222222CCCC
    222222CCCC4444444444444444444444444
    222222CCCC
    222222CCCC
    222222CCCC
    .
    .
    .
    // 2 is m   
    // C is cS              <--- actual data is a variety of letters and numbers 
    // 4 is dataIn

    im almost 100% sure that it is because i am using getline wrong, but i dont know how i am using it wrong... im really stuck on this one.

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    question,

    you are trying to output to the same file you read in?

    show me where you open the file (your code)

    what is your file suppose to look like
    after your output?



    Here is a basic way to read and display the
    file, i dont know how much help it will be since
    you seem to what to do something with individual
    parts of the source file.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string filestring;
    	ifstream ins("file.txt");
    	if(ins.is_open)
    	{
    		while(getline(ins, filestring, '\n'))
    		{
    			cout << filestring << endl << endl;
    		}
    	}
    	cin.get();
    	return 0;
    }
    Last edited by ILoveVectors; 07-27-2005 at 10:14 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    You cannot add C style strings (character arrays). To combine strings in that way you must use strcat. Use the C++ string class instead.

    Your use of getline is wrong. First, you should not use eof() to control the loop. Check the return value of getline. If it is false, then you've reached eof or some other error.

    Second, there are two forms of getline. One works with C style strings like you have. That one takes the number of characters in the buffer as the second argument. So where you have '\n', the compiler is interpreting that as a number (probably 10 - the ASCII value of '\n') corresponding to how many characters to read in. The delimiter is the third argument to the function. There is another form of getline that works with C++ strings, that doesn't require the buffer size because strings grow automatically. That version takes the stream as the first parameter.

    So my suggestion is to switch to the C++ string class and use the string version of getline. ILoveVectors has given a good example of this.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    16
    thanks a whole bunch guys, it works like a charm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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