counting lines in text file

T

tolkien

Hi,i have this problem.I'm reading strings from a text file and i want
to know in which line of the file i am.
What i did is this:

char string[20];
while(!feof(text_file)){
fscanf(text_file,"%s",string);
for(i=0;string!=' \0 ';i++)
if(string==' \n ' )
lineCounter++;
}

and it doesn't work.It works for any other character except ' \n '.
Any thoughts??
Thanks!!!
 
F

fred.l.kleinschmidt

Hi,i have this problem.I'm reading strings from a text file and i want
to know in which line of the file i am.
What i did is this:

char string[20];
while(!feof(text_file)){
fscanf(text_file,"%s",string);
for(i=0;string!=' \0 ';i++)
if(string==' \n ' )
lineCounter++;

}

and it doesn't work.It works for any other character except ' \n '.
Any thoughts??
Thanks!!!


scanf and fscanf swallow newlines (as well as all other whitespace).
Use fgets() instead
 
K

Keith Thompson

tolkien said:
Hi,i have this problem.I'm reading strings from a text file and i want
to know in which line of the file i am.
What i did is this:

char string[20];
while(!feof(text_file)){
fscanf(text_file,"%s",string);
for(i=0;string!=' \0 ';i++)
if(string==' \n ' )
lineCounter++;
}

and it doesn't work.It works for any other character except ' \n '.
Any thoughts??


You need to read section 12 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.

I'll assume that the rest of your program is ok (proper declaration of
main, ``#include <stdio.h>'', text_file opened properly, result of
fopen() checked, lineCounter initialized to 0, etc.).

Testing feof() at the top of a loop is not the right way to read a
file; this is explained in question 12.2.

fscanf with a "%s" format doesn't read a line; it reads a string
delimited by whitespace (including newlines). It reads an arbitrarily
long string, and you've provided only 20 characters to store it. If
the file begins with "abcdefghijklmnopqrstuvwxyz", fscanf will attempt
to store all 26 characters plus a terminating '\0' in your
20-character array. This will cause your program to crash *if you're
lucky*. If you're not lucky, it will just clobber some other memory
and the error won't show up until the most inconvenient possible
moment.

Making your array bigger doesn't solve the problem. No matter what
arbitrary size you choose, it can be exceeded (with arbitrarily
horrible results) unless you have complete control over your input.

Since all you're doing is counting '\n' characters, you don't need to
store multiple characters from the file. You can just read a single
character at a time, testing each one. Since the standard I/O
routines perform buffering, this shouldn't be significantly slower
than reading in larger chunks.

But if you do want to read a full line at a time, fgets() is a good
starting point. fgets can't read an arbitrarily long line, but you
can tell it (in fact you *must* tell it) to read at most some
specified number of characters. If the input line exceeds the size of
your buffer, it obviously can't store the entire line -- but its
behavior is well defined. Read the documentation (a man page, your
online help system, or a good textbook or tutorial) to find out how to
use it.

There are also a number of different (non-standard) routines that can
read an arbitrarily long line, dynamically allocating however much
memory is needed to hold it, but I don't thik you need that kind of
thing yet.
 
M

Malcolm McLean

Use this idiom for reading a file character by character

int ch; /* note, int */

while ( (ch = fgetc(fp)) != EOF)
{
if(ch == '\n')
linecount++;
}

If you actually need to process lines rather than just count them, you'll
need fgets() or a function that allocates the buffer for you.
 
T

tolkien

thank you keith very much.
Yes the rest of my program is ok :)
What i'm trying to do is find how many times a word exists in a file
and in which lines,
that's why i used fscanf because i want to stop at the whitespace.I
thought that fscanf could
read '\n'.I think i can do it now.
 
K

Keith Thompson

tolkien said:
thank you keith very much.
Yes the rest of my program is ok :)
What i'm trying to do is find how many times a word exists in a file
and in which lines,
that's why i used fscanf because i want to stop at the whitespace.I
thought that fscanf could
read '\n'.I think i can do it now.

The simplest way to read a file is one character at a time.

If that doesn't work (because you need more information in memory tha
one character), assuming it's a text file, usually the best approach
is to read a line at a time and work on each line as a string in
memory.

fgets() is the simplest way to read lines (*don't* use gets()), but
you have to think about how to handle very long lines.
 
U

user923005

thank you keith very much.
Yes the rest of my program is ok :)
What i'm trying to do is find how many times a word exists in a file
and in which lines,
that's why i used fscanf because i want to stop at the whitespace.I
thought that fscanf could
read '\n'.I think i can do it now.

K&R2, p20. has the pedagogic solution.

Just for fun, I wrote a wc before looking it up and I was surprised
how similar it was to the book version (though I used function calls
and the book uses char by char comparison for figuring out character
types).

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int i,m,c,w,l;
m = c = w = l = 0;
while ((i = getchar()) != EOF) {
if (isspace(i) || ispunct(i) || isalnum(i)) {
c++;
if (i == '\n')
l++;
if (isspace(i) || ispunct(i)) {
if (m) {
m = 0;
w++;
}
} else {
m = 1;
}
} else {
puts("Binary files not allowed.");
puts("This version allows only whitespace, punctuation,");
puts("letters and numbers.");
exit(EXIT_FAILURE);
}
}
printf("c=%d,w=%d,l=%d\n", c, w, l);
return 0;
}

When you are done, show us what you came up with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top