?
Solved

read words of a text file line-by-line C code

Posted on 2009-12-24
5
Medium Priority
?
317 Views
Last Modified: 2012-05-08
I want to read all the words of a text file line by line.
With the code below input file must have only 2 words per line.
What changes should I do, to read all the words of each line?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){
    FILE* fp;
    char buff[80];
    char* p;
    fp=fopen("input1.txt","r");
    if (fp==NULL) exit(1);
    
    while(fgets(buff, 80, fp)){
       
       p = strtok(buff, " ");
       printf("%s ", p);
       p = strtok(NULL, " ");
       printf("%s ", p);getchar();
       }
       fclose(fp);
       getchar();
       return 0;
}

Open in new window

0
Comment
Question by:xiromdim
  • 3
  • 2
5 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26118035
I think make the buffer bigger. It is 80 in your code. It should be as big as the file.
I think, with your code, it will be the minimal change
 
 
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26118043
Sorry, the buffer should be as long as the longest line plus 1.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26118068
:)

getchar();
You have this function after each two words.
 
0
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 26118084
Just loop on the remaining words in the line. You were close. See code for one idea. This collects words, and does not care if the words are on one line or another.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){
    FILE* fp;
    char buff[80];
    char* p;
    fp=fopen("input1.txt","r");
    if (fp==NULL) exit(1);
    
    while( fgets(buff, 80, fp) ) {
		p = strtok(buff, " \n"); // get first word in line
		printf("%s ", p);
		while( (p = strtok(NULL, " \n") ) ) {
			printf("%s ", p);
		}
    }
	fclose(fp);
	return 0;
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 26118095
Oh, I removed the getchar, which I am guessing you use to debug. Do you have access to a debugger? If so, maybe we can help you learn to use it. It will make your debugging much easier.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question