|
-
Jan 3rd, 2002, 09:05 PM
#1
Thread Starter
Fanatic Member
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;
}
Alcohol & calculus don't mix.
Never drink & derive.
-
Jan 3rd, 2002, 10:08 PM
#2
Lively Member
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.
-
Jan 4th, 2002, 12:05 AM
#3
Thread Starter
Fanatic Member
Sorry, it used to be 'char num' but I changed it. What's a space of CR?
Alcohol & calculus don't mix.
Never drink & derive.
-
Jan 4th, 2002, 07:08 AM
#4
I think he means newline = \n
-
Jan 4th, 2002, 07:58 AM
#5
Addicted Member
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];
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|