Array problem

This is a discussion on Array problem within the C Programming forums, part of the General Programming Boards category; Please help? I need to read house details from a file , store them in an array and then print ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    Array problem

    Please help? I need to read house details from a file , store them in an array and then print the list of houses. I think what I have below is correct but how do I add a new house?
    Help here would be much appreciated. Thanks.


    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    FILE *fp;
    char filename[15];
    int ch;


    printf("\nEnter filename ?\n");
    gets(filename);

    if((fp=fopen(filename,"r"))==NULL)
    {
    printf("\nUnable to open'%s':%s",filename);
    return 1;
    }

    while((ch=getc(fp))!=EOF)
    putchar(ch);
    fclose(fp);
    return 0;

    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Two main things:
    Please use fgets
    while((ch=getc(fp))!=EOF)
    You should use fgetc(fp) to get a character from a file. This function is also mentioned in the link I proposed.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>You should use fgetc(fp) to get a character from a file
    The getc function is equivalent to fgetc, except that if it is implemented as a macro, it may evaluate stream more than once, so the argument should never be an expression with side effects.
    carter, read this

    >>gets(filename);
    Avoid this function, see this which you can easily adapt to be used for files as well as keyboard input.

    >>store them in an array
    There's no array in your code for this?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21