Thread: Matrix in a file. Help!

  1. #1
    Registered User Arminel's Avatar
    Join Date
    Dec 2006
    Location
    Giurgiu
    Posts
    6

    Matrix in a file. Help!

    I write a matrix in a text file as it shows the example:

    2 5 2 5 6
    2 3 4 6 3
    2 5 6 8 6

    I want to find out the number of lines and columns. I've started writing the code but I can't determine the number of lines.
    Code:
    #include <stdio.h>
    
    int main()
    
    {
    FILE *f;
    f = fopen ("mat1.txt", "r");
    int spaces=0, endl=0;
    char c, d;
    do {
          fscanf(f, "%c", &c);
          if ( c == ' ' )
              spaces++;
         }
    while ( c != '\n' );
    fseek ( f, 0, SEEK_SET );
    do {
          fscanf(f, "%c", &d);
          if ( d == '\n' )
              endl++;
         }
    while (!EOF);
    printf("The matrix has %d columns and %d lines.", spaces+1, endl+1);
    fclose(f);
    return 0;
    }
    What did I do wrong ?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Well what's the output?

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while (!EOF);
    EOF is a constant. !EOF is the same as
    Code:
    while (0);
    Kurt
    Last edited by ZuK; 12-24-2006 at 06:34 AM.

  4. #4
    Registered User Arminel's Avatar
    Join Date
    Dec 2006
    Location
    Giurgiu
    Posts
    6
    The matrix has 5 columns and 1 lines.
    This output is for the example I gave.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Both your file reading loops need to be of this form

    Code:
    while ( fscanf(f, "%c", &c) != EOF ) {
      // do something with c
    }
    In the first loop, you could do this to get out of the loop when you see the first newline
    if ( c == '\n' ) break;
    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.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    user fgets() function to find out the number of lines.
    Code:
        while(fgets(str,80,fp) != NULL)
            line++;
    this gives you the number of lines which u have read. By keeping tack of line variable

    to find out the num of col it depends on the way each file is stored

    example: if your line is stored with out the space like
    Code:
    12345
    just count the num of char in a line. or if you have like this
    Code:
    1 2 3 4 5
    use this tokenize the string

    ssharish2005

  7. #7
    Registered User Arminel's Avatar
    Join Date
    Dec 2006
    Location
    Giurgiu
    Posts
    6
    Thanks! Work fine now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM