Thread: File reading and arrays

  1. #1
    Unregistered
    Guest

    Angry File reading and arrays

    I have this program and it suppose to read a file for an 52 char string. Then process the string. The y and n are use to process diffrent things I can't even get it read form the file right. It goes through the loop 12 time to get 5 lines of data. Is there easerway to get the info aout of the file then i'm using?

    Latter on i need you use the n and y, but when i print the array out it does everthing 2 times( prints it twice, instead of 5 lines i get 10 lines of the same code) like there extra code when i put a for loop to print the first 31 char out.

    here the file

    YNYNYNYNYNYNYNYNYNYNYNYNYNYN1302 Coleridge Avenue
    NYNYNYNYNYNYNYNYNYNYNYNYNYNYNY2311 Avenue A West
    YYYYYNNNNNYYYYYNNNNNYYYYYNNNNN10656 Rondo Avenue
    NNNNNNNNNNNNNNNNNNNNNNNNNNNNNN111 N. Montgomery Rd.
    NNNNNYYYYYNNNNNYYYYYNNNNNYYYYYNE 53rd Street


    here the code


    #include <stdio.h>

    int main()
    {
    char Apart[52] , i ;
    FILE *input_file ;
    int counter = 1, n = 1 ;



    /* If no file the give error */
    if ( ( input_file = fopen( "i:\\c\\files\\Apts.dat", "r" ) ) == NULL )
    fprintf(stderr,"The input file could not be opened.\n");
    else


    while(!feof(input_file))
    {
    n++;
    fgets(Apart, 52, input_file);
    printf("%s", Apart);
    }
    printf("%d", n);

    fclose (input_file); /* Close file */

    return ;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    try opening the file in "rb" mode(as it's a binary file....I suppose)

  3. #3
    Unregistered
    Guest
    Thanks you very much, that works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding reading data from file into arrays
    By vutek0328 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 09:20 AM
  2. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM
  3. reading from file to arrays
    By supernormal2 in forum C Programming
    Replies: 1
    Last Post: 12-08-2002, 03:58 PM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM
  5. Reading from a .txt file or use arrays.
    By []--JOEMAN--[] in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2001, 07:55 AM