Sign in to follow this  
CJWR

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

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
well, my file is the following:


qwerty

asdfgh

zxcvbn



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.

Share this post


Link to post
Share on other sites
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');

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites


#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[i]);

while(getline(read,s)){
cout << s << endl;
x++;
}

read.close();

}
cout << x << " lines" << endl;
}

Share this post


Link to post
Share on other sites
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[i]);
while(getline(in,s)){
//cout << s << endl;
fileLines++;
}
cout << fileLines << " lines in " << argv[i] << 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[i]);
int fileLines = std::count(istbuf_itr(in), istbuf_itr(), '\n');
cout << fileLines << " lines in " << argv[i] << endl;
total += fileLines;
}
cout << total << " lines total" << endl;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this