how to read a file?

M

_mario.lat

I have a file whith four field in each line:
string, int, int, float.
For example:

age 3 5 3.5
avr 6 7 3.6
....
....

how can I read this file doing something like that:

for each line do{
str=get the string (or word)
int1=get the first int
int2=get the second int
f=get float
.....
elaborate str,int1,int2 and f
.....
}

Thank you in advance, Mario.
 
S

Sharad Kala

_mario.lat said:
I have a file whith four field in each line:
string, int, int, float.
For example:

age 3 5 3.5
avr 6 7 3.6
...
...

how can I read this file doing something like that:

for each line do{
str=get the string (or word)
int1=get the first int
int2=get the second int
f=get float
....

Post some code where you have tried the problem. Then people will be happier
to help you here. Seriously, if you search http://groups.google.com , you
will find the problem been answered several times.

Best of luck.

-Sharad
 
J

John Harrison

_mario.lat said:
I have a file whith four field in each line:
string, int, int, float.
For example:

age 3 5 3.5
avr 6 7 3.6
...
...

how can I read this file doing something like that:

for each line do{
str=get the string (or word)
int1=get the first int
int2=get the second int
f=get float
....
elaborate str,int1,int2 and f
....
}

Thank you in advance, Mario.

Like this

ifstream file("some_file");
string str;
int int1, int2;
float f;
while (file >> str >> int1 >> int2 >> f)
{
...
}

But there is not much error checking in the above code. If you want to check
the input then things get more complicated.

john
 
R

Rolf Magnus

_mario.lat said:
I have a file whith four field in each line:
string, int, int, float.
For example:

age 3 5 3.5
avr 6 7 3.6
...
...

how can I read this file doing something like that:

for each line do{
str=get the string (or word)
int1=get the first int
int2=get the second int
f=get float
....
elaborate str,int1,int2 and f
....
}

Thank you in advance, Mario.

std::string line;
while (std::getline(thefile, line))
{
std::string str;
int int1, int2;
float f;
std::istringstream s(line);

if (! (s >> str >> int1 >> int2 >> f))
{
//broken line
}
}

if (!thefile.eof())
{
//error
}
 
M

_mario.lat

Thank you to all to answer me an for the precious time that all of you spend
to answer me.
Thank you, Mario.
 
M

_mario.lat

I can do that when in th file there are numbres.
The problem is to get the "word" (the first elemente of the row)
The file is like:
word1 3 4 2.4
age 3 7 4.6
kdshl 4 8 4.5
....


When I write
filedata>>string1;

it says I'm wrong!

How can I do?

Thank you in advance, Mario.
 
J

John Harrison

_mario.lat said:
I can do that when in th file there are numbres.
The problem is to get the "word" (the first elemente of the row)
The file is like:
word1 3 4 2.4
age 3 7 4.6
kdshl 4 8 4.5
...


When I write
filedata>>string1;

it says I'm wrong!

What does it say? Post the real code with the real error message.

john
 
M

_mario.lat

this is the code I write (followed by errors):


# include <stdio.h> // standard input output
# include <stdlib.h> // standard library
# include <fstream.h> // for file functions
# include <time.h> // for getting time
# include <math.h> // mathematical
# include <string.h> // string utilities
# include <iostream.h> // for i/o
# include <string>


class distanza{
private:
public:
distanza();
};

distanza::distanza()
{

ifstream fdati("c:\\tabelladistanze.txt", ios::nocreate);
if(!fdati) {cout<<"errore\n"; exit(1);}
else cout<<"file "<<fdati<<" aperto\n";

std::string str;
int x1,x2;
float f;


fdati>>str;
cout<<str;
}


void main()
{
distanza d();
}

---------------------------

--------------------Configuration: distanze - Win32
Debug--------------------
Compiling...
distanze.cpp
C:\Documents and Settings\...\codice_cpp\distanze.cpp(38) : error C2679:
binary '>>' : no operator defined which takes a right-hand operand of type
'class std::basic_string said:
' (or there is no acceptable conversion)
C:\Documents and Settings\....\codice_cpp\distanze.cpp(39) : error C2679:
binary '<<' : no operator defined which takes a right-hand operand of type
'class std::basic_string said:
' (or there is no acceptable conversion)
Error executing cl.exe.

distanze.obj - 2 error(s), 0 warning(s)
-------------------------------

I tied to read only the string ... if I try to read string and numbers the
error increase!

Thank you for your help.
Mario.
 
M

_mario.lat

Problem resolved. I write the code (for other people who will need it):

# include <iostream>
# include <fstream> // for file functions
# include <string> // string utilities
# include <climits>
# include <cstdlib>
# include <conio.h>


using namespace std;

void main()
{
char x; int card, val;
int count, tot=0;

//apro il file dove sono i dati per le distanze.
ifstream fdati("c:\\tabelladistanze.txt");
if(!fdati) {cout<<"errore\n"; exit(1);}
else cout<<"file "<<fdati<<" aperto\n";


string str;
int x1,x2;
float f;
while(fdati>>str>>x1>>x2>>f)
{
cout<<str<<" x1:"<<x1<<" x2"<<x2<<" f"<<f<<"\n";;

}
}





Thankyou to all!

Mario.
 
J

John Harrison

this is the code I write (followed by errors):


# include <stdio.h> // standard input output
# include <stdlib.h> // standard library
# include <fstream.h> // for file functions
# include <time.h> // for getting time
# include <math.h> // mathematical
# include <string.h> // string utilities
# include <iostream.h> // for i/o
# include <string>

You are using the wrong header files.

# include <fstream.h> // for file functions
# include <iostream.h> // for i/o

iostream.h and fstream.h are not legal C++ header files. Any attempt to
mix them with legal C++ header file (like <string>) will have
unpredictable effects. Change to

# include <fstream>
# include <iostream>

and change

ifstream to std::ifstream, cout to std::cout etc. etc.

john
 
J

John Harrison

ifstream fdati("c:\\tabelladistanze.txt", ios::nocreate);

Also ios::nocreate is not legal C++, get rid of it, you don't need it.

It seems you are learning C++ from an out of date source.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top