c++ question about getting a char from a file.

Started by
5 comments, last by Zahlman 18 years, 9 months ago
ok, here is my code that i'm having problems with:

	char char_inputed;
	fin >> char_inputed;
	while(char_inputed != '\n'){
		cout << char_inputed;
		fin >> char_inputed;
	}
now, the goal is to read chars from the file until we read in the end of the line, and then stop reading in chars. However, this reads the whole file in and keeps printing the last char read in forever. what part of this am i doing wrong? thank you for your time and help.
Charles Reed, CEO of CJWR Software LLC
Advertisement
That would typically happen if there is not newline in the file. You should also check if you are passed the end of the file; it might be that >> will keep returning the character last read.

Greetz,

Illco
well, my file is the following:

qwertyasdfghzxcvbn


there is a few returns in there i think :)

and i know about the end of file, i put in a cin.eof() to stop it from never stopping, but what i really need is to know when a line ends. i'm trying to build a line counter program.
Charles Reed, CEO of CJWR Software LLC
You should know that using stream insertion/extraction operators preforms formatted I/O, anyways:

char c = '\0';while(fin >> c && c != '\n' && std::cout << c)    ;


Quote:Original post by CJWR
i'm trying to build a line counter program.


you could do something like:

typedef std::istreambuf_iterator<char> istbuf_itr;//...std::cout << std::count(istbuf_itr(fin), istbuf_itr(), '\n');
Quote:Original post by snk_kid
You should know that using stream insertion/extraction operators preforms formatted I/O, anyways:

char c = '\0';while(fin >> c && c != '\n' && std::cout << c)    ;


Quote:Original post by CJWR
i'm trying to build a line counter program.


you could do something like:

typedef std::istreambuf_iterator<char> istbuf_itr;//...std::cout << std::count(istbuf_itr(fin), istbuf_itr(), '\n');


as nice as that is, your code didn't work either.
Charles Reed, CEO of CJWR Software LLC
#include <iostream>#include <fstream>using namespace std;int main(int argc,char *argv[]){    int x = 0;    string s = "";    ifstream read;        for(int i = 1; i < argc; i++){        read.open(argv);                while(getline(read,s)){            cout << s << endl;            x++;        }                read.close();            }    cout << x << " lines" << endl;}

Fixed up a tiny bit:

#include <iostream>#include <fstream>#include <string>using namespace std;int main(int argc,char *argv[]){  int total = 0;  string s = "";      for(int i = 1; i < argc; i++) {    int fileLines = 0;    ifstream in(argv);    while(getline(in,s)){      //cout << s << endl;      fileLines++;    }    cout << fileLines << " lines in " << argv << endl;    total += fileLines;  }  cout << total << " lines total" << endl;}


Oh, and with proper context, snk_kid's thing certainly does work (compiled and tested locally):

#include <iostream>#include <fstream>using namespace std;int main(int argc,char *argv[]){  typedef std::istreambuf_iterator<char> istbuf_itr;  int total = 0;      for(int i = 1; i < argc; i++) {    ifstream in(argv);    int fileLines = std::count(istbuf_itr(in), istbuf_itr(), '\n');    cout << fileLines << " lines in " << argv << endl;    total += fileLines;  }  cout << total << " lines total" << endl;}

This topic is closed to new replies.

Advertisement