Open, Read, Print an input file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pxllyte
    New Member
    • Nov 2006
    • 3

    Open, Read, Print an input file

    I'm a beginner for C programming. For a part of my project, I have to take an input file from the user and be able to print it onto the screen. This is what I have so far:

    Code:
    char filename[10];
    
    FILE *ifp;
    printf("Enter the name of the file to analyze :");
    scanf("%s", filename);
    printf("\n");
    
    ifp = fopen(filename, "r");
    if (ifp == NULL)
    {
       fprintf(stderr, "Could not open file: '%s' ", filename);
       exit (-1);
    }
    
    int c = fgetc(ifp);
    
    while (c != EOF)
    {
       printf("%c", ifp);
    }
    
    fclose(ifp);
    However... I don't think the while loop is working properly especially when I get an argument error. How can I fix this?

    Thanks!
  • onlivimal
    New Member
    • Nov 2006
    • 3

    #2
    Originally posted by pxllyte
    I'm a beginner for C programming. For a part of my project, I have to take an input file from the user and be able to print it onto the screen. This is what I have so far:

    Code:
    char filename[10];
    
    FILE *ifp;
    printf("Enter the name of the file to analyze :");
    scanf("%s", filename);
    printf("\n");
    
    ifp = fopen(filename, "r");
    if (ifp == NULL)
    {
       fprintf(stderr, "Could not open file: '%s' ", filename);
       exit (-1);
    }
    
    int c = fgetc(ifp);
    
    while (c != EOF)
    {
    
    
    //printf("%c", c);
    //c=fgetc(ifp);
     /*  printf("%c", ifp);*/
    
    }
    
    fclose(ifp);
    However... I don't think the while loop is working properly especially when I get an argument error. How can I fix this?

    Thanks!

    check the above commented lines!! guess it works

    Comment

    Working...