Thread: help for newbie with text file reading

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    1

    help for newbie with text file reading

    Guys, I'm starting to learn C, and I'm trying to make a simple program to read input from another, and there is something strange. this is the input:

    Code:
    north: 7278678.49381985
    south: 7276518.88888158
    east: 733491.08009739
    west: 730341.50485467
    rows: 8
    cols: 4
    527

    and this is the code

    Code:
    #include <stdio.h>
    
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    FILE *entrada;
    
    char arquivo_in[30],arquivo_out[30];
    
    double north_coord,south_coord,east_coord,west_coord,grid_ns,grid_ew;
    int n_rows,n_cols,elev,i,j;
    
    
    
    int main(int argc, char *argv[])
    {
    if(argc != 2)
    {
    printf("Input should be of the form \'svxdem [input] [output]\'");
    return 1;
    }
    
    
    
    
    FILE *entrada = fopen(argv[1], "r");
    if(entrada == NULL)
    {
    printf("Couldn't open %s", argv[1]);
    return 1;
    }
    
    
    /* read ascii file limits */
        
    
    fscanf(entrada,"north: %lf\n",&north_coord);
    fscanf(entrada,"south: %lf\n",&south_coord);
    fscanf(entrada,"east: %lf\n",&east_coord);
    fscanf(entrada,"west: %lf\n",&west_coord);
    fscanf(entrada,"rows: %d\n",&n_rows);
    fscanf(entrada,"cols: %d\n",&n_cols);
    
    fscanf(entrada,"%d",&elev);
        
    /* calculate grid spacing */
    
    grid_ns=(north_coord-south_coord)/n_rows;
    grid_ew=(east_coord-west_coord)/n_cols;
    
        
    printf("north: %7.8lf\n",north_coord);
    printf("south: %7.8lf\n",south_coord);
    
    printf("east: %6.8lf\n",east_coord);
    printf("west: %6.8lf\n",west_coord);
    printf("rows: %d\n",n_rows);
    printf("cols: %d\n\n",n_cols);
    printf("grid_ns: %lf\n",grid_ns);
    printf("grid_ew: %lf\n\n",grid_ew);
    
    printf("elev: %d\n",&elev);
     
     
    
    printf("\n\n*** Done ***\n");
    
    fclose(entrada);
    
    
    
    return 0;
    
    }

    and this is the output:

    Code:
    north: 7278678.49381985
    south: 7276518.88888158
    east: 733491.08009739
    west: 730341.50485467
    rows: 24
    cols: 35
    
    grid_ns: 89.983539
    grid_ew: 89.987864
    
    elev: 134519392
    
    
    *** Done ***
    So, something is wrong, since it is reading 'elev' as a really bizarre value.
    Do you see anything I missed?

    tks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not something you missed. Something you've added.
    Code:
    printf("elev: %d\n",&elev);
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by guano_geo
    So, something is wrong, since it is reading 'elev' as a really bizarre value.
    Do you see anything I missed?
    You're printing its address, not its value.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > FILE *entrada = fopen(argv[1], "r");
    Normal C doesn't permit embedded declarations, so this is either C99 code or C++ code.
    Check your compiler documentation.

    Also, if you're using gcc as your compiler, use
    -W -Wall
    as additional command line arguments. For one thing, they spot printf/scanf errors.
    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
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    Quote Originally Posted by Salem
    > FILE *entrada = fopen(argv[1], "r");
    Normal C doesn't permit embedded declarations, so this is either C99 code or C++ code.
    erm... i'm asking because i always use
    Code:
    FILE* fin, fout = fopen(...);
    if (!fin) return(1); 
    etc...
    or
    Code:
    FILE* fin, fout;
    if ((fin = fopen(...)) == NULL) return(1);
    and they both work under my C89 compiler in C mode
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Look where the variable was declared moonlord
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  4. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  5. reading from a text file help......
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 12:51 PM