Thread: ReadData txt file

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    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 !
    Last edited by RyanC; 05-04-2019 at 06:47 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    > 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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    > 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***
    Last edited by RyanC; 05-04-2019 at 08:10 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    > 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?
    Last edited by RyanC; 05-04-2019 at 08:44 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    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
    Last edited by RyanC; 05-04-2019 at 10:06 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    > 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
    Last edited by RyanC; 05-04-2019 at 10:39 AM.

  10. #10
    Banned
    Join Date
    Apr 2015
    Posts
    596
    The problem is: while reading from input.txt file, all the data on the output is messed up! and that's weird!!

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    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. #13
    Registered User
    Join Date
    Feb 2019
    Posts
    717
    Quote 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. #14
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by flp1969 View Post
    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. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,552
    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 subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-21-2017, 10:48 AM
  2. Replies: 6
    Last Post: 12-28-2015, 03:36 AM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM

Tags for this Thread