Jump to content

How to read a .txt file?

- - - - -

  • Please log in to reply
19 replies to this topic

#1
skastu01

skastu01

    Newbie

  • Members
  • PipPip
  • 27 posts
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!

#2
programble

programble

    Newbie

  • Members
  • PipPip
  • 28 posts
C or C++?

#3
hetra

hetra

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 297 posts
  • Location:Australia
  • Programming Language:C, C++, PHP, Python, Delphi/Object Pascal, Assembly
  • Learning:Python, Assembly
This was a post from when I was still a beginner in C++:

http://forum.codecal...oding-help.html

Edited by Hunter100, 29 July 2010 - 12:50 PM.
typo


#4
skastu01

skastu01

    Newbie

  • Members
  • PipPip
  • 27 posts
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
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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
skastu01

skastu01

    Newbie

  • Members
  • PipPip
  • 27 posts
Great thank you! Do you know how to remove carriage returns, tabs and additional white space greater than 1 character in the output?

#7
programble

programble

    Newbie

  • Members
  • PipPip
  • 28 posts
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
skastu01

skastu01

    Newbie

  • Members
  • PipPip
  • 27 posts
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
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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
programble

programble

    Newbie

  • Members
  • PipPip
  • 28 posts
'\r' is carriage return and '\t' is tab.

#11
skastu01

skastu01

    Newbie

  • Members
  • PipPip
  • 27 posts
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
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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