473,394 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Reading a file that looks like this..

Hello, I have a text file with following format:
number one_word
string
number number char string

There may be several lines of the last type (number number char
string), the number of such lines should correspond to the first number
in the file. Anyway, how do I read such a file if I need the numbers
stored in ints, the single char stored in a char and the strings (words
or sentences) stored in a std::string? I tried:

static void
read_and_display_file(ifstream& file)
{
int num = 0;
string str;

/* We know the first line is a number and a single word. */
file >> num >> str;

cout << num << " " << str << endl;

/* We know the second line is a string (that contain spaces). */
getline(file, str);

cout << str << endl;

int a = 0;
int b = 0;
char c = '\0';

/* Now we loop the number of times as stated at the beginning of
the file. The line all have the following format:
int int char some_string_that_may_contain_spaces */
for(int i = 0; i < num; ++i)
{
/* First we read the two ints and the char. */
file >> a >> b >> c;

/* The we read the remainder of the line into a std::string. */
getline(file, str);

cout << a << " " << b << " " << c << " " << str << endl;
}
}

My testfile:
4 word
a sentence
1 2 a dld dld
3 4 b dlsl dlfefef
5 6 c slspod sejdjd
7 8 d lsll spd dslddd
The output when I run my testprogram:
$ ./foo.exe
4 word

0 0
0 0
0 0
0 0

Please help me solve this, thanks!

Feb 1 '06 #1
2 1571

Eric Lilja wrote:
Hello, I have a text file with following format:
number one_word
string
number number char string

There may be several lines of the last type (number number char
string), the number of such lines should correspond to the first number
in the file. Anyway, how do I read such a file if I need the numbers
stored in ints, the single char stored in a char and the strings (words
or sentences) stored in a std::string? I tried:

static void
read_and_display_file(ifstream& file)
{
int num = 0;
string str;

/* We know the first line is a number and a single word. */
file >> num >> str;
Ok, here's the error. I need to call getline() here to skip to the
second line
then I can call it again to actually read the contents of the second
line.
Determined by printing what file.peek() returned here.

cout << num << " " << str << endl;

/* We know the second line is a string (that contain spaces). */
getline(file, str);

cout << str << endl;

int a = 0;
int b = 0;
char c = '\0';

/* Now we loop the number of times as stated at the beginning of
the file. The line all have the following format:
int int char some_string_that_may_contain_spaces */
for(int i = 0; i < num; ++i)
{
/* First we read the two ints and the char. */
file >> a >> b >> c;

/* The we read the remainder of the line into a std::string. */
getline(file, str);

cout << a << " " << b << " " << c << " " << str << endl;
}
}

My testfile:
4 word
a sentence
1 2 a dld dld
3 4 b dlsl dlfefef
5 6 c slspod sejdjd
7 8 d lsll spd dslddd
The output when I run my testprogram:
$ ./foo.exe
4 word

0 0
0 0
0 0
0 0

Please help me solve this, thanks!


/ Eric

Feb 1 '06 #2

Eric Lilja wrote:
[snipped code]


I think you shouldn't mix getline and extractors too much like you do.
The state of the stream gets very confusing to manage if you do this.
Also you're missing a lot of error handling.

Further, I wouldn't rely on the value you read from the file as a for
index parameter. Instead read until the end of the file.

Here's how I would rewrite your function:

static void read_and_display_file(ifstream& file)
{
string line;
stringstream ss;

int num = 0;
string str;

getline( file, line );
ss.str( line );

// We know the first line is a number and a single word.

ss >> num >> str;
cout << num << " " << str << endl;

// We know the second line is a string (that contain spaces).
getline(file, str);
cout << str << endl;

int a = 0;
int b = 0;
char c = '\0';

// Now we loop the number of times as stated at the beginning of
// the file. The line all have the following format:
// int int char some_string_that_may_contain_spaces
while( getline( file, line ))
{
ss.clear();
ss.str(line);

// First we read the two ints and the char.
ss >> a >> b >> c;

// The we read the remainder of the line into a std::string.
getline(ss, str);

cout << a << " " << b << " " << c << " " << str << endl;
}
}
Cheers,
Andre

Feb 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
7
by: Shane | last post by:
Hi, Thanks in advance for the help. I have been to many websites and tried several solutions to my problem, but have fixed part of it. It's time to come humbly to the newsgroups for help :-) ...
3
by: Carl Lindmark | last post by:
*Cross-posting from microsoft.public.dotnet.languages.csharp, since I believe the question is better suited in this XML group* Hello all, I'm having some problems understanding all the ins and...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
1
by: Paul Fi | last post by:
I have this problem with .NET remoting: my remote class is called RemoteHandler which implements an interface called IEazyRemoting which has only one method to be implemented which is my server...
1
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
3
by: Eroc | last post by:
I'm new to XML files so I'm kinda lost here. I found some example code on reading an XML file. My objective is simple. Read the whole XML file into memory. Here is part of my code: Private...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
1
by: anii | last post by:
Hi! I'm working on a piece of code for school. =) in C++. (I've read the rules and understand that i can't put all of the code here and ask for all of the code) I've got all (or most) of the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.