-
file reading
i have a file data.txt
and i want to read the data form the file and write them to screen
file data is :
1 2 3 ;
Code:
int i,j=0,dat[256];
ifstream fin;
char *line,*data;
fin.open("data.dat");
fin.getline(line,256);
while(fin)
{
i = 0;
while(line[i] != ' ; ')
{
k = 0;
while(line[i] != ' ') //while the char is not space ex: 10 5 4;
{
data[k] = line[i];
}
k = 0;
dat[k] = atoi(data); //is that true??
}
}
}
-
all this stuff is redundant and useless for you...
if your class looks like this:
1 2 3 ;
then do this and you're don:
Code:
#include<fstream>
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
ifstream fin("data.txt");
char temp;
if(fin.fail())
{
cout << "Can't open file for input!!! \n";
exit(1);
}
else
{
fin.get(temp);
while(temp != ';')
{
cout << temp << endl;
fin.get(temp);
}
}
return 0;
}
since (like you said) you only want to read a character from the file and display it on the screen this would be sufficient for you...
matheo917
-
This is a small one (am a newbie :) ) reading every character from a textfile and displaying it on the screen.
Not sure if this is what you are looking for but it might help.
Code:
# include <iostream.h>
# include <fstream.h>
# include <stdlib.h>
void main()
{
fstream bestand;
char letter;
bestand.open ("c:\\data.txt",ios::in);
while(!bestand.eof())
{
bestand.get(letter);
cout<<letter;
}
bestand.close();
}
ooops, forgot the code tag sorry:)
-
>void main()
main only returns int, anything else is wrong and undefined.
>bestand.open ("c:\\data.txt",ios::in);
Be sure to check that this succeeded.
>while(!bestand.eof())
This is the wrong way to use eof(), it's better to use the input call as a loop condition since they tend to return correct boolean values:
Code:
while ( bestand.get ( letter ) )
Code:
#include <iostream>
#include <fstream>
int main()
{
char c;
std::ifstream in ( "c:\\data.txt" );
if ( in.is_open() ) {
while ( in.get ( c ) )
std::cout<< c <<std::flush;
in.close();
}
}
-Prelude
-
Thanks for the correction.