Reading fixed width columns from txt file...

I'm trying to read columns in a text file that won't always have a delimiter but they are a fixed width...

I've created a testcase here: https://ideone.com/1bfIi

I'm trying to display the data but I'm getting nothing instead...
Could someone please help me understand what I'm doing wrong?

Thanks!

Note: This is for a homework assignment and I've coded much of it (removed from the testcase, of-course), this is one part I can seem to work out though and I've been stuck on it for over an hour... The professor stated that we were supposed to use char arrays instead of strings and that getline() should also be used.
You see "In loop!" because 'endl' calls flush(). The other output don't have an 'endl' and hence no flush. The command 'pause' is not found and so the program ends without showing the non flushed output (maybe it shows but you're not able to see it)
closed account (DSLq5Di1)
http://www.cplusplus.com/reference/iostream/istream/getline/
If the function stops reading because this size is reached, the failbit internal flag is set.

Hence you fail on the first loop, use cin.clear() after each getline(), alternatively:-
http://www.cplusplus.com/reference/iostream/istream/get/

1
2
3
4
5
6
7
char thisRecord[4][256]; // should be 5?
...
        cin.getline( thisRecord[0], 5, '\n' );  // 1
        cin.getline( thisRecord[1], 20, '\n' ); // 2
        cin.getline( thisRecord[2], 9, '\n' );  // 3
        cin.getline( thisRecord[3], 9, '\n' );  // 4
        cin.getline( thisRecord[4], 9, '\n' );  // ..5? run time error. 

The size you pass to getline() should account for a null character to terminate the string (eg. if you wish to read in 5 characters, your size should be 6).

Your fixed widths look a little off too..
  5           20             9        9        9
|---||------------------||-------||-------||-------|
    1 Item one                   11       12       13
    2 Item two                   12       13       14
    3 Item three                 13       14       15

Also, are you supposed to be reading all of the input into character arrays? as it does not make much sense to treat the numbers as strings.
sloppy, right now I'm just trying to get access to the data period... Once I get things displaying, I'll worry about datatypes... :-/

I've been stuck on this for like 5 hours and I'm going insane... :-/ Is there a better way to read the fixed width columns? Could you demonstrate an example of it if so as I've tried a bazillion things since making the initial post and at this point, the only way I'm going to learn is by seeing something that actually works as I'm about to throw my towel in and give up on it soon...

Also, [0] - [4] is 5, is it not? Don't you count 0 and 1?
Last edited on
closed account (DSLq5Di1)
http://ideone.com/ketWg

NoviceCDev wrote:
Also, [0] - [4] is 5, is it not? Don't you count 0 and 1?

I think you're confusing indexing an array vs declaring the size of an array. int someArray[4], someArray has 4 elements, 0, 1, 2, 3.
Awesome, however the modified code you submit still doesn't work... :-/
closed account (DSLq5Di1)
What's not working?
I'm still not able to get any output... Nothing is displayed...

-- Edit --
I had another error in another part of my code. What you posted has actually worked!
Thanks

How would I trim the whitespace? I've not been able to find anything like PHP's trim() func... :-/
Last edited on
closed account (DSLq5Di1)
On the link I posted..? took awhile to compile so you may need to refresh it (try cloning and re-submitting if that doesn't help), or something else?

.. and please provide as much information as possible in your posts! I don't fancy turning this forum into an instant messaging conversation.
Last edited on
closed account (DSLq5Di1)
There is no standard C/C++ trim function, and without using std strings or algorithms it can be a hassle to implement,

1
2
#include <cctype>
#include <cstring> 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void trim(char* str)
{
	char* first_char = str;

	while (*first_char && isspace(*first_char))
		++first_char;

	if (!(*first_char)) // empty string
	{
		*str = '\0';
		return;
	}

	char* last_char = str + strlen(str) - 1;

	while (last_char != first_char && isspace(*last_char))
		--last_char;

	*(++last_char) = '\0';

	if (first_char != str)
		memmove(str, first_char, last_char - first_char + 1);
}
Last edited on
Topic archived. No new replies allowed.