ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to write an algorithm to count the number of lines in a text file, and this is all being done by one function, and the number of lines is being kept track of by a counter:
I wrote the following algorithm:
The text file is in the format:
Code:
Word1
Word2
Word3
Word4
The words have a max length of 15 characters and there is only one word on each line, therefore WORDARRAYSIZE is an integer value of 16.
For some reason when I pass wordCounter into this function, and print it out for test purposes, it returns the value 0. The parameter int wordCounter is initialized to 0 before this function is encountered, so im guessing the loop simply does not execute, but why?
Good point, however, when I try this it also does not modify the value of wordCounter, and it remains 0. I think it may be a loop problem, but I'm not exactly sure why... hm....
Maybe im not validly checking the words in the text file?
Actually returning an int instead of passing it back through the params seems like the best way to do something here. Counting newlines will break something else cause this function seems to also be used to load the words into the wordArray.
Code:
int loadWords(FILE* inputFile,char *wordArray[16])
{
int lines=0;
while(fgets(wordArray[lines],wordArray[lines].length,inputFile))lines++;
return lines;
}
Is this C or C++? If it is C++, you could use a std::ifstream. If it is C, maybe look into fscanf. Generally, fgets might not be doing what you want it to. Just experimentally, try doing !fgets.
Ah okay, very good idea, but I'm actually having trouble reading the words in from the start...
I went back to trace through step by step, and it seems they never even read in properly,
even though the commands that I am doing are correct.
It also appears that the text file deletes each time I test the executable, and by this I mean
that the file's contents are simply blank after each run. I have no idea why. I opened, I closed,
added a bunch of printf("%s");
Hm... This is definately some tricky code, but I will try !fgets() when and if I get the input
streaming to work.
I typed in a complete example that works.
Save this off to t.c and type "sh t.c" and it will compile itself and run; then count the number of lines in the file t.c, its 26 when I did it.
Code:
//usr/bin/gcc -O2 -Wall -o t t.c; ./t; exit 0
#include <stdio.h>
#include <stdlib.h>
#define WORDARRAYSIZE 256
void CountNumberOfLines(FILE *inputFile, char wordArray[], int *wordCounter) {
while (fgets(wordArray,WORDARRAYSIZE, inputFile)) ++(*wordCounter);
}
int main(int argc, char *argv[]) {
char buf[WORDARRAYSIZE];
FILE *f;
int count;
f=fopen("t.c","r");
if (!f) return 1;
count=0;
CountNumberOfLines(f,buf,&count);
fclose(f);
printf("%d\n",count);
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.