1. ## ReadData txt file

Hi ! I'm struggling this problem and I didn't find a solution for it.
I'm trying to read data from txt which the data of the txt file is about having size of matrix and coordination (x,y) according to the size where the value "1" located. it might have more than matrix on the same txt file. for example"
Code:
```444
12
23
33
555
12
23
33
44```
the size of the matrix is always signed as 3 digits,
for example in the first line of txt file there's 444, it says that the matrix size of matrix 4*4 and the value number one is located on matrix at coordination: 12 23 33
for second matrix which it's in size 5 as shown in fifth line in txt file 555, the value of number one in that matrix is located at coordination: 12 23 33 44 .

the output is to print all the matrixs that txt file input have with proper value as written in txt file.

I'm stuck in and not moving forward , How can I assign matrixs by coordination that are given in txt file? thanks alot!!

I've written this code for reading data from txt file but didn't work well!(wrong output)

Code:
```int main()
{
int i,j;
double value[10000][10000];
FILE *archivo;
archivo = fopen("matrix.txt","r");
if (archivo == NULL)
exit(1);
i=0;
while (feof(archivo) == 0)
{
fscanf( archivo, "%lf %lf %lf", &value[i][0],&value[i][1],&value[i][2]);
printf("%10.0f %f %f\n", value[i][0], value[i][1], value[i][2]);
i++;
}

printf("My matrix:\n");
for(j = 0; j < i; j++)
printf("%10.0f %10.3f %10.3f\n", value[j][0], value[j][1], value[j][2]);

fclose(archivo);
return 0;
}```

THANKS for your help !

2. > the size of the matrix is always signed as 3 digits,
> for example in the first line of txt file there's 444, it says that the matrix size of matrix 4*4
Are your matrices always square, or can they be rectangular?
In other words, does it matter which of the three 4's you take to be the size.

Can the size be larger than 10?
If not, then double value[10000][10000]; is just massive overkill.
Not to mention that it probably blows the stack before you've even begun.

How do you know that say 444 and 555 are matrix sizes, and not a coordinate?

3. Originally Posted by Salem
> the size of the matrix is always signed as 3 digits,
> for example in the first line of txt file there's 444, it says that the matrix size of matrix 4*4
Are your matrices always square, or can they be rectangular?
In other words, does it matter which of the three 4's you take to be the size.

Can the size be larger than 10?
If not, then double value[10000][10000]; is just massive overkill.
Not to mention that it probably blows the stack before you've even begun.

How do you know that say 444 and 555 are matrix sizes, and not a coordinate?
For your first question, matrics always square. (doesn't matter), the first digit is indicating the size of the matrix, the other two digits are don't care, they are just found for declaration that "444" is a boundary that says new matrices with size 4 initiated ..

For your second question, Yes can be larger than 10.
For your last question, because the matrix are always square so we just need 2D span in other words just (x,y) and there's no z axis (x,y,z). the pattern of the input is like this:
*** size of matrices ** following by
*** coordination of that matrices**
*** size of matrices *** followed by
***coordination of that matrices***

4. > For your second question, Yes can be larger than 10.
OK, so how do you know that say 444 is a size and not a coordinate?

And how do you know whether 444 should be interpreted as 4,44 or 44,4.

As far as file formats go, it sucks.

5. Originally Posted by Salem
> For your second question, Yes can be larger than 10.
OK, so how do you know that say 444 is a size and not a coordinate?

And how do you know whether 444 should be interpreted as 4,44 or 44,4.

As far as file formats go, it sucks.
I understand .. but what's confusing me how can I read lines until I face 3 digits like "444" which implies new matrices will begin ..
I mean like this:
444
12
13
23
555
what's the condition that I can use for splitting the begging of new matrices ..here if I face 555 I understand that new matrices would be initiated ..so what's the condition for?

6. Code:
```int value;
while ( fscanf( archivo, "%d", &value) == 1 ) {
if ( value >= 100 && value <= 999 ) {
int size = value % 10;
} else {
int x = value / 10;
int y = value % 10;
}
}```
But you haven't said what happens when any size of coordinate is greater than 10.

7. Originally Posted by Salem
Code:
```int value;
while ( fscanf( archivo, "%d", &value) == 1 ) {
if ( value >= 100 && value <= 999 ) {
int size = value % 10;
} else {
int x = value / 10;
int y = value % 10;
}
}```
But you haven't said what happens when any size of coordinate is greater than 10.
I really appreciated your help ! (for your question what happened when any size of ..greater than 10 , lets assume it wouldn't happen -an assumption-.

I appreciate once again your code, but checking your code, I have my own code and I need to know what's the problem:
here's my code:
Code:
```int main() {
int i, j, F, D, size, num, row1, coL1;
FILE *filep;
double *ptr = NULL;
double x;

if((filep==fopen("NAME.txt", "r")) == NULL) {
printf("\nFailed to open File.\n");
return -1;
}
while (!feof(filep)) {
if (fscanf(filep, "%u", &num) == 1) {
printf("%d\n", num);
}
if (fscanf(filep, "%u%u%u", &size, &F, &D) == 3) {
printf("%d%d%d\n", size, F, D);

if (fscanf(filep, "%u%u", &row1, &coL1) == 2) {
printf("%d%d\n", row1, coL1);
}
}
}
}```
the input:
in NAME.TXT is:
Code:
```100
3 1 3
2 3
1 2
2 2
3 2
3 2 0
1 3
2 3
3 3 3
1 3
2 3```
the output I got
Code:
```100
313
23
1
222
32
3
201
32
3
333
13
2```
the output isn't the same as the input, its values the same, but it's not organized .. any clue how to output the same output as the input?
I want to print what's found on NAME.txt on my screen with the same method that I'm using to print in my code!

what's wrong with my code? I want my output to be the same as the input NAME.txt and not messed up like what I've shown

thanks alot

8. > while (!feof(filep))
Have you read the FAQ?
Why it's bad to use feof() to control a loop - Cprogramming.com

I showed you the safe way, and you just went ahead and ignored it anyway.

> the input:
>in NAME.TXT is:
Oh right, so NOW it's all space separated.

Garbage in, garbage out.

Next time, be sure what you're posting.
I don't appreciate having time wasted on dead ends.

> printf("%d%d%d\n", size, F, D);
Yeah, try putting spaces between all your %d's, so 1 2 3 actually looks like 1 2 3 and not 123.

9. Originally Posted by Salem
> while (!feof(filep))
Have you read the FAQ?
Why it's bad to use feof() to control a loop - Cprogramming.com

I showed you the safe way, and you just went ahead and ignored it anyway.

> the input:
>in NAME.TXT is:
Oh right, so NOW it's all space separated.

Garbage in, garbage out.

Next time, be sure what you're posting.
I don't appreciate having time wasted on dead ends.

> printf("%d%d%d\n", size, F, D);
Yeah, try putting spaces between all your %d's, so 1 2 3 actually looks like 1 2 3 and not 123.
but how can I tell my compiler when reach the end of the file as stop? so by !feof() command I can tell the pc to stop when there's no data on the txt file.
second off, how can I solve the problem? can you show me by a code to understand more? I've already tried .
my problem isn't having spaces in 123 or 1 2 3, my problem if you can see my output txt isn't the same as input txt, all the data messed up ..

in my input.txt the fourth line is 1 2 and in my output txt the fourth line is 1
so here's the prblem, the number 2 is written down in line fifth

thanks alot

10. The problem is: while reading from input.txt file, all the data on the output is messed up! and that's weird!!

11. Post your actual input file.

Not your attempt to read it and print it.

> but how can I tell my compiler when reach the end of the file as stop?
That's what the == 1 means on the fscanf() call.
It's testing that an integer was read from the file successfully.

Did you read the FAQ?
Did you understand the FAQ?

> my problem if you can see my output txt isn't the same as input txt, all the data messed up ..
Well that's down to you writing bad format strings without any spaces.

12. Originally Posted by Salem
Post your actual input file.

Not your attempt to read it and print it.

> but how can I tell my compiler when reach the end of the file as stop?
That's what the == 1 means on the fscanf() call.
It's testing that an integer was read from the file successfully.

Did you read the FAQ?
Did you understand the FAQ?

> my problem if you can see my output txt isn't the same as input txt, all the data messed up ..
Well that's down to you writing bad format strings without any spaces.
but I've wrote this code:
Code:
```int main() {
int i, j, F, D, size, num, row1, coL1;
FILE *filep;
double *ptr = NULL;
double x;

if((filep==fopen("NAME.txt", "r")) == NULL) {
printf("\nFailed to open File.\n");
return -1;
}
while (!feof(filep)) {
if (fscanf(filep, "%u", &num) == 1) {
printf("%d\n", num);
}
if (fscanf(filep, "%u%u%u", &size, &F, &D) == 3) {
printf("%d %d %d\n", size, F, D);

if (fscanf(filep, "%u%u", &row1, &coL1) == 2) {
printf("%d %d\n", row1, coL1);
}
}
}
}
```

and still not having the correct output, still getting messed data on the output !!

13. Originally Posted by RyanC
but I've wrote this code:
...
and still not having the correct output, still getting messed data on the output !!
It means, of course, your code is wrong... and Salem already explained why...

14. Originally Posted by flp1969
It means, of course, your code is wrong... and Salem already explained why...
So how can I solve the problem?! I know that salem explained it but it's not obvious for me!

15. Have you found and read any documentation for fscanf()? You're not using this function correctly, this is the start of your problems. When you find and read some documentation of fscanf() pay particular attention to the information on the format specifiers, if you use the wrong specifier for the type of variable you invoke undefined behaviour.

Also read the link about why it is usually wrong to use EOF to control a data entry loop.

You also still need to post a sample of your input file (inside code tags please to preserve formatting).

Popular pages Recent additions