LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2005, 12:50 PM   #1
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Reading the number of Lines in a File


Hello,

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.

Code:
void CountNumberOfLines (FILE* inputFile, char wordArray [ ], int wordCounter)
{
   while (fgets (wordArray, WORDARRAYSIZE, inputFile) != NULL)
   {
      wordCounter++;
   }
}
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?

Any ideas?

Thanks,
Mistro116
 
Old 11-20-2005, 01:40 PM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
The function argument wordCounter is passed in by value, it should be passed in by reference.
Code:
void CountNumberOfLines (FILE* inputFile, char wordArray [ ], int *wordCounter) {
   while (fgets(wordArray,WORDARRAYSIZE, inputFile)) ++(*wordCounter);
}
When calling the function pass the address of the integer using '&'.
 
Old 11-20-2005, 01:45 PM   #3
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
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?

Mistro116
 
Old 11-20-2005, 02:42 PM   #4
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Why can you not count the newline charecters?
 
Old 11-20-2005, 03:05 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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;
}

Last edited by tuxdev; 11-20-2005 at 03:06 PM.
 
Old 11-20-2005, 03:46 PM   #6
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
I changed the code to a pointer modification, then I changed it to returning an int as you suggested:

Code:
void CreateWordList (char inputFileName [ ])
{
   FILE* inputFile;
   
   inputFile = fopen (inputFileName, WRITEFILE);
   
   if (inputFile == NULL)
   {
      fprintf(stderr, "Error - Cannot open %s for reading!\n", 
	      inputFileName);
      
      exit (-1);
   }
   
   int counter = CountNumberOfWords (inputFile);
   
   printf("\n%d\n\n", counter);
}

int CountNumberOfWords (FILE* inputFile)
{
   char wordArray [WORDARRAYSIZE];
   int wordCounter = 0;
   
   while (fgets (wordArray, WORDARRAYSIZE, inputFile) != NULL)
   {
      wordCounter++;
   }
   
   return wordCounter;
}
The number of words are still not being counted correctly, and the function just returns the value 0.

Any more ideas I can try?

Mistro116
 
Old 11-20-2005, 05:18 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.

Last edited by tuxdev; 11-20-2005 at 05:19 PM.
 
Old 11-20-2005, 05:33 PM   #8
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
This is C.. pretty obvious... and fgets is exactly what im doing in the code posted previously...

Thanks.

Mistro116
 
Old 11-20-2005, 05:42 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
thought so, but you can never be exactly sure from snippets. With emphasis: use !fgets(). Expecting infinite loop.

Last edited by tuxdev; 11-20-2005 at 05:44 PM.
 
Old 11-20-2005, 05:45 PM   #10
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
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.

Thanks a ton.
Mistro116
 
Old 11-20-2005, 05:53 PM   #11
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Yeah, the problems with opening the FILE is probably causing this as well. How are you opening the FILE exactly?
 
Old 11-20-2005, 05:55 PM   #12
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
a simple call:

inputFile = fopen("filename.dat", "r");

I'm completely clueless now, I've started to e-mail all my friends hahaha.

Thanks.
Mistro116
 
Old 11-20-2005, 06:10 PM   #13
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Stumped as well. "rt"? Really shouldn't make a difference. Ask a CS Professor nearby?
 
Old 11-20-2005, 07:05 PM   #14
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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;
}
 
Old 11-20-2005, 09:08 PM   #15
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Quote:
Originally posted by randyding

Save this off to t.c and type "sh t.c" and it will compile itself and run;
Something completely new for me. Where do I go to learn about this. Manual sh?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cat: output specific number of lines mikeshn Linux - Software 8 01-24-2017 11:08 AM
count total number of lines in several files xushi Programming 5 11-12-2005 04:42 PM
Reading random lines in c++? twirl Programming 7 10-30-2005 10:11 AM
output number of blank lines tjgadu Linux - Newbie 7 06-09-2005 04:01 PM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 06:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration