Thread: cout not printing full string

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    10

    cout not printing full string

    My programme reads a line from a text file using

    in_file_stream1.getline(read_record,255);

    Each line in the file contains four fields, the numerical fields are right justified which means there will be a variable number of spaces between the fields. An example is
    80-09V 7.00 470632.30 419554.80

    Under certain circumstances the programme will detect an error and I want to display the line where the error occurred using cout in a console.

    If I do this with

    cout << read_record;

    only the first part of the line is displayed - up to the first space. How can I get cout to display the full string?

    Grateful for advice.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Try something like this.
    Code:
    unsigned int ln = 1;
    while (in_file_stream1.good()) {
        in_file_stream1.getline(read_record, 255);
        ln++;
    }
    if (in_file_stream1.eof()) {
        cout << "\nEnd of file reached.\n";
    } else if (in_file_stream1.fail()) {
        cout << "\nError condition at line: " << ln << endl;
    }

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    How's read_record defined? It would also be interesting to take a look at the read loop code.

    Meanwhile you can use in_file_stream1.eof(), in_file_stream1.fail() and in_file_stream1.bad() to diagnose the possible error. Trying to output the stream with cout is not useful because chances are it is corrupted already.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Definition of read_record:


    Code:
    char read_record[256]
    Loop structure:

    Code:
    	while ((loop_check = in_file_stream1.peek()) != EOF )
    	{
    		in_file_stream1.getline(read_record,255); etc etc etc
            }
    Within the loop there is a function to parse the read_record and do some calculations based on the different fields. The two last numerical fields are x and y map coordinates and the error I need to spot is if two consecutive records have the same x and y coordinates, because this will cause division by zero and crash the programme. The value of getting cout to print the whole line if this occurs, instead of just the line number, is that you can then paste the cout output into a text editor search box and go straight to the problem line.

  5. #5
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Code:
    std::string win;
    std::getline(f,win);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Replies: 2
    Last Post: 04-16-2004, 04:58 AM
  4. c++ string input
    By R.Stiltskin in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2003, 04:25 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM