How can i read some number from a text file and then go back to read some other information?
All i know, is read it from the beginning to the end, but even when i check something for ex if(fgetc(in)!=...) goes forward one character, and all this messes up my plans :(
Here's the problem i'm trying to solve:

Once, a gardener ordered different kind of flowers which were delivered one after the other. These flowers were
planted line by line by the gardener on the basis of their arriving in an num matrix shape garden.
Write a program that will calculate in which column of the garden will be the most flowers with the same color. If
there will be more solutions you have to write down the first solution (the smallest column index).

Input
The first line contains the number of the test cases. In the corresponding line of each case a string and two positive
integers (separated by spaces) are written. The characters of the string represent the colors of the flowers, the first integer
is the value of n (number of the lines) and the second one is m (number of the columns).

Output
Your program has to print (for each test case) two numbers and one character separated by spaces: the number of
column (the number of start column is 1) in which the most unicolered flowers are, and the number of most flowers with
the same color, and what is this color.

Sample Input: G.IN
2
ABCBBADCBDBA 4 3
KFPSSKPFFKPSSPFKPSPK 5 4

Sample Output: Standard output
2 3 B
3 4 P

See in the input, i must order the characters in an nxm matrix, but the size is unknow until the end of the line, so i would need to go back and read it :(
I already tought to add it in an array, and then read it back, but there's another problem, the colors can be numbers too, so i don't even know the type before i check the first character...
Any help would be greatly appreciated!

Edited 5 Years Ago by Incubator: n/a

You can use the fseek function (in stdio.h) to set the file position (byte offset).

Comments
Completely unnecessary. Bad advice.

Since you didn't post any code, we have no way of knowing what you did wrong. This is a very simple task and how you tried to do it will tell us what to suggest. Otherwise you get suggestions like the one above by rxlim

Since you didn't post any code, we have no way of knowing what you did wrong. This is a very simple task and how you tried to do it will tell us what to suggest. Otherwise you get suggestions like the one above by rxlim

alright, sorry about that, here it is, not much, but here's where i got stuck, if i could make that matrix, i might be able to make this task

btw. here's the real input

7
ABCBBADCBDBA 4 3
KFPSSKPFFKPSSPFKPSPK 5 4
ABCDEFBCDFAABAFBBADEEEFDAABADA 6 5
asdasfgasdartaghajafadasaeratagahahafadasadafgahagafadasadafagahajaaagafadafgafagahajafadasasadafagahagafadasafaha 6 19 
KFLKFLRKRFLRFLKRFLKRFLLLLKKKKKKKKKKKKKKKKKKKKKKKKRRRRRRRRRRRRRRRRRRRRRRFFLKFRLKLRFLFRKLFRLKRFRFRFRFRFRFRFFFFFFFFFFFFFFFFFFFFFFFLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLRFLRLRFLRFKKKKKKKKLKLRKLKRFKLRFLLLLLLLLLLRRRRRRRRRRRRRRFRLFRFLRFLRFLKKKKKKKKKKKKRLFKLRFKLRKLLLLLLLLLLLLLLLLLLRKRLFLFFF 23 12
123456789123456879654785412365478965412365478965412365478965412365412365478965412365478965412365477896541236547899877774563214569855478544123652222236541236521458785412541258789658745874521475221452214522155215525989654223123652455878965214587741125889633214587896511236658778996544478966232333222112258896641232336589785411254555554587896523322145452144552114522115544778523365588995475632154521212254412541254125412547411122123654447854785458745874552145214587965236521123456789784 21 23
123123123 3 3


here's my code

int main()
{
	int i, j, k=0, test, n, m;
	char **garden;
	char c, colors[100];
	FILE *be;
	be=fopen("G.IN", "r");
	fscanf(be, "%i", &test);

	for(i=0; i<test; ++i){
		if(fgetc(be)>64 && fgetc(be)<91) //i tried to check if it's with letters or numbers, but still not good because it goes forward a char
		fscanf(be, "%s %i %i", colors, &n, &m); //tried to read it temporarly to jump over it and get the numbers
	}

	//system("pause");
	return 0;
}

Edited 5 Years Ago by Incubator: n/a

int main()
{
	int i, j, k=0, test, n, m;
	char **garden;
	char c, colors[100];
	FILE *be;
	be=fopen("G.IN", "r");    // How do you know the file was opened correctly? 
                                  // You didn't check for any errors.

	fscanf(be, "%i", &test);  // You read the first integer but the \n is
                                  // still left in the file. I suggest you read
                                  // a full line and convert the value to integer

	for(i=0; i<test; ++i){
		if(fgetc(be)>64 && fgetc(be)<91) 
                                  // This statement reads the next two characters and
                                  // checks the first to be > 64 and the second < 91
                                  // You probably want this to be after you read the 
                                  // line then check the first character in 'colors'

		fscanf(be, "%s %i %i", colors, &n, &m); 
                                  // Again, read the entire line then convert.
	}

	//system("pause");
	return 0;
}

Thanks, that push was actually VERY helpful.

I'm almost done, but there's an algorith to compare the colors and i'm not sure how should i do it, nothing good comes in my mind and i actually wasted pretty much time with it

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

void scan_garden(char **, int, int);
int main()
{
	int i, j, k, l, test, n, m;
	char **garden;
	char line[10200]; //that's the most characters a line can have because of the
                          //n<100 & m<100 limit, but i don't really like this solution
	FILE *be;
	be=fopen("G.IN", "r");
	fscanf(be, "%i\n", &test);

	for(i=l=0; i<test; ++i, l=0){
		fgets(line, sizeof(line), be);
		sscanf(line, "%*s %i %i", &n, &m);
		if(n>=100 || m>=100) continue;

		garden=(char**)calloc(n, sizeof(char*));
		for(k=0; k<n; ++k){
			garden[k]=(char*)calloc(m, sizeof(char));
		}
		
		for(j=0; j<n; ++j){
			for(k=0; k<m; ++k){
				garden[j][k]=line[l++];
			}
		}

		scan_garden(garden, n, m);
		free(garden);
	}

	//system("pause");
	return 0;
}

void scan_garden(char **garden, int n, int m)
{
	int i, j;
	for(i=0; i<m; ++i){
		for(j=0; j<n; ++j){
			//algorithm to count the flower colors...
		}
	}
	printf("\n\n");
}

btw, how do i check the fopen function for errors?
i have also put in the \n in the scanf to take it out

Thanks, that push was actually VERY helpful.

Good.

I'm almost done, but there's an algorith to compare the colors and i'm not sure how should i do it, nothing good comes in my mind and i actually wasted pretty much time with it

Since I don't know the algorithm, nothing comes to my mind either.

btw, how do i check the fopen function for errors?

Try This
And while you're at it, This Too

i have also put in the \n in the scanf to take it out

:icon_question:

This question has already been answered. Start a new discussion instead.