-
Reading in characters
I want to write a program that opens a file with ifstream and reads each character in the file and prints it out. This is what I have so far:
PHP Code:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char a;
ifstream fin;
fin.open("test.txt");
fin >> num;
cout << num; // Works OK
while (!fin.eof()) // ?
{
// What goes in here?
}
fin.close();
return 0;
}
-
Not sure where your 'num' name is coming from... but anyway fstream works like cin for the most part... you would most likly need a deliminating character between each char input... or you could simply create a character array... then fin to the array.. then you can use each character individually... but it will end input to the array when a space of CR is reached.
char a[256];//or however big you need it
fin >> char ;
cout<< char[0];
cout<<char[1];
ect...
or use a loop to do the same.
-
Sorry, it used to be 'char num' but I changed it. What's a space of CR?
-
I think he means newline = \n
-
you can also do this:
Code:
char ch;
while(the_file.get(ch))
cout << ch;
or make a 2-dimensional char array....
Code:
char info[100][20];
for(int i=0;i<500;i++)
the_file >> info[i];