I need assistance in how to read a .txt file and print the content to screen. I would like to read it character by character because I do not know what the file may contain.
Thanks!
19 replies to this topic
#1
Posted 29 July 2010 - 12:30 PM
|
|
|
#2
Posted 29 July 2010 - 12:42 PM
C or C++?
#3
Posted 29 July 2010 - 12:49 PM
This was a post from when I was still a beginner in C++:
http://forum.codecal...oding-help.html
http://forum.codecal...oding-help.html
Edited by Hunter100, 29 July 2010 - 12:50 PM.
typo
#4
Posted 29 July 2010 - 01:13 PM
In C++. I need help with how to read in each character at a time. I have started with the following sample code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main ( )
{
ifstream in_stream;
ofstream out_stream;
#Variable Declarations
in_stream.open(“infile.txt”);
return 0;
)
Edited by Roger, 30 July 2010 - 12:41 PM.
added "code" tag
#5
Posted 29 July 2010 - 01:39 PM
std::ifstream in("file_to_open.txt");
char* ch = new char;
while (!in.eof()) {
in.read(ch, 1);
std::cout << ch;
}
delete ch;
More here.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#6
Posted 29 July 2010 - 02:02 PM
Great thank you! Do you know how to remove carriage returns, tabs and additional white space greater than 1 character in the output?
#7
Posted 29 July 2010 - 02:18 PM
For carriage returns and tabs, it's a simple matter of checking to see if ch is \r or \t. For more than one space, you probably want to keep a variable with the last character, and then check to see if the last one was a space, and don't output the current one if it is also a space.
#8
Posted 30 July 2010 - 10:40 AM
Hi - This is what I have so far. What is /r or /t? My logic was to check that if char is ‘ ‘, then print it, and then loop reading in chars that follow that are also ‘ ‘(but not print them), until I find one that isn’t a ‘ ‘. Having difficulty coding this logic though. Any help is appreciated.
int main()
{
ifstream indata;
char a;
int num;
indata.open("data9.txt");
if(!indata) {
cerr << "Error: The file could not be opened" << endl;
cin>>num;
exit(1);
}
indata >> a;
while ( !indata.eof() ) {
indata >> a;
cout<<a;
}
cout<<endl;
cin>>num;
indata.close();
return 0;
}
Edited by Roger, 30 July 2010 - 12:40 PM.
added code tags
#9
Posted 30 July 2010 - 11:20 AM
When you read data, check what it is
indata >> a;
if (a == ' ')
// do this
/* you can also use switch statement */
switch (a) {
case ' ':
// do that
break;
case 'a':
// do this and that
break;
default:
// don't do that and this
}
Also, small note on files"" said:
You aren't required to use the close command as it will automatically be called when the program terminates, but if you need to close the file long before the program ends, it is useful.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#10
Posted 30 July 2010 - 11:21 AM
'\r' is carriage return and '\t' is tab.
#11
Posted 30 July 2010 - 11:26 AM
Hey - Thanks. I understand the case switch logic, but did not use it because I did not know how to read and interpret 2 characters immediately one after the another? Also how do I base it on what the previous character's value was?
#12
Posted 30 July 2010 - 12:11 PM
You have to remember what the previous character was, use another variable and when you've processed current character save it as previous and read next one.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









