Just getting back into c (I've been dosing assembly for the last few months), so don't laugh at me :S I'm trying to read an input file. The fist line contains how many lines to read. The rest of the lines contain 3 words. Here's an example:

3
Hello, World and
Hello, DaniWeb and
Hello, anything else

I want to read the first line, and loop it that many times. This works. Then I want to read the next line, load the words into seperate variables and print them.
Here's my code:

#include <stdio.h>

FILE *infile;
char word1[256];
char word2[256];
char word3[256];

int firstline();
void nextline();
int quit();

int main() {
	int max;
	int count = 0;

	infile = fopen("input.txt", "r");
	max = firstline();
	while(count != max) {
		printf("%s %s %s", word1, word2, word3);
		count++; }
	return quit(); }

int firstline() {
	int num;

	fscanf(infile, "%d", &num);
	return num; }

int quit() {
	fclose(infile);
	getchar();
	return 0; }

void nextline() {
	fscanf(infile, "%s %s %s", word1, word2, word3); }

Any ideas?

Recommended Answers

All 3 Replies

Your not calling the function that should get the line of input. Also your using global variables which is bad practice.

#include <stdio.h>

int firstline();
void nextline();
int quit();

int main() {
FILE *infile;
    char word1[256+1];
    char word2[256+1];
    char word3[256+1];
    
	int max;
	/*Count is 1 otherwise you get an endless loop.*/
	int count = 1;

	infile = fopen("input.txt", "r");
	/*You need to test to see if file actually exists.*/
	
	max = firstline();
	while(count != max){
	    /*Iv just used word1 but you need to use multidimensional arrays here. As your in a loop*/
	    fscanf(infile, "%[\n]\n", word1);
    }
    
	return quit(); 
}

int firstline() {
	int num;

	fscanf(infile, "%d", &num);
	return num; 
}

int quit() {
	fclose(infile);
	getchar();
	return 0; 
}

void nextline() {
	fscanf(infile, "%s %s %s", word1, word2, word3); 
}

I have not used your nextline function as it wont work if you want to use a loop to get the values from the file. You need to have a multidimensional array like words[3][256+1]; and use that in your loop instead.

Thanks alot Kenji, and your right:) . This was just a quick and dirty program, and I was not even going to use functions. I added them in latter, so I just quickly used globals. But why do I need multi dimentional arrays? I'm just planning to write them to the screen (and my more formal program, do something else, but line by line.) Lastly why does count need to be 1? If I do it your way, it'll start at 1, and miss the last line.
Heres my loop:

int max = 30;
int count = 0;
while(count != 30) {
    //do something; }

Thanks for reminding me to call nextline(), its working now:)

As your not sure how many values you want take from the file you need to either use malloc to create dynamic arrays or have a const or #define to set your arrays. Using multidimensional arrays you create a array that is as big as you need as well as how long you need. Check this link.

In this case an array that is 30 elements long and contains 256 chars + 1 for the NULL byte. A while loops until the condition is false, so you can keep calling different elements and inserting values into it.

int i = 0;
while(count != max){
    fscanf(infile, "%[]", words[i][]);
    i++;
    count++;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.