Thread: File I/O

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    1

    File I/O

    Hello all!

    Working on an assignment and I've hit TWO stumbling blocks. I now have to take the first line from a .txt file and use that number to determine the size of rows to create in my program. I am using fscanf but the program hangs. How can I read in this number, store it and use it in a for loop?

    The second issue is after reading this number I have to print each number in the .txt file under a different column. (Note that it skips a line after reading the row count.) The .txt file is set up as follows:

    9

    1
    3
    6
    4
    7
    8
    8
    6
    1

    The output should look sort of like:

    Number
    ​1
    3
    6
    4
    7
    8
    8
    6
    1

    Thanks for your help!

    Here's my attempt:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>

    typedef struct{
    int number;
    } Entry;

    int main(int argc, const char * argv[])
    {

    char filename[80];
    FILE *fp;
    int rows;


    input = 0;
    while(!input){
    printf("\nEnter the name of the file: ");
    scanf("%s", filename);

    if((fp = fopen(filename, "r")) == NULL) {
    fprintf(stderr, "Unable to open %s\n", filename);
    printf("Please make sure the specified file exists.");
    }
    else input = 1;
    }

    if(fscanf(fp, "%d", &rows) != 1) {
    fprintf(stderr, "Error reading from %s\n", filename);
    exit(-2);
    }
    printf("\nNumber);

    for(i = 0; i < rows; i++)
    {
    fscanf(fp, "%d", &entries[i].number);
    printf("%d", entries[i].number);
    }

    res = fclose(fp);
    if(res == 0) {
    printf("File closed\n");
    }
    else
    {
    printf("Unable to close file\n");
    }

    fp = NULL;

    verify=0;
    while(!verify){
    printf("\nContinue? (y = yes, n = no):");
    scanf(" %c", &repeat);
    if(repeat == 'y' | repeat == 'n')
    verify=1;
    else printf("\nIncorrect response.");
    }
    return 0;
    }
    Last edited by j44; 04-18-2014 at 03:47 PM. Reason: Added attempt

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    +1 for posting your code
    -1 for failing to use [code][/code] tags. Read the following links.
    Forum guidelines.
    Posting code? Read this first!
    Homework policy. Not that this necessarily applies here, but it's good to read anyway.

    Here's what your code should look like:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    
    typedef struct{
        int number;
    } Entry;
    
    int main(int argc, const char * argv[])
    {
    
        char filename[80];
        FILE *fp;
        int rows;
    
    
        input = 0;
        while(!input){
            printf("\nEnter the name of the file: ");
            scanf("%s", filename);
    
            if((fp = fopen(filename, "r")) == NULL) {
                fprintf(stderr, "Unable to open %s\n", filename);
                printf("Please make sure the specified file exists.");
            }
            else input = 1;
        }
    
        if(fscanf(fp, "%d", &rows) != 1) {
            fprintf(stderr, "Error reading from %s\n", filename);
            exit(-2);
        }
        printf("\nNumber);
    
        for(i = 0; i < rows; i++)
        {
            fscanf(fp, "%d", &entries[i].number);
            printf("%d", entries[i].number);
        }
    
        res = fclose(fp);
        if(res == 0) {
            printf("File closed\n");
        }
        else
        {
            printf("Unable to close file\n");
        }
    
        fp = NULL;
    
        verify=0;
        while(!verify){
            printf("\nContinue? (y = yes, n = no):");
            scanf(" %c", &repeat);
            if(repeat == 'y' | repeat == 'n')
                verify=1;
            else printf("\nIncorrect response.");
        }
        return 0;
    }
    Make sure you use an editor with syntax highlighting. You'll notice that here, you forgot a " on line 34 (maybe a copy-paste error).

    Your code as posted can't hang because it wont even compile. Then compile with errors turned all the way up:
    Code:
    $ make foo
    gcc -g -Wall -std=c99 -o foo foo.c -lpthread -lm -lpq
    foo.c: In function ‘main’:
    foo.c:18:5: error: ‘input’ undeclared (first use in this function)
    foo.c:18:5: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:36:9: error: ‘i’ undeclared (first use in this function)
    foo.c:38:27: error: ‘entries’ undeclared (first use in this function)
    foo.c:42:5: error: ‘res’ undeclared (first use in this function)
    foo.c:53:5: error: ‘verify’ undeclared (first use in this function)
    foo.c:56:23: error: ‘repeat’ undeclared (first use in this function)
    All those are basic errors -- undefined variables. Please provide their definitions so we can compile your code and help you with your problem.

    Also, give a clearer description of what the code should do. What do you mean by print them each in a different column. Different how? How do you determine which column to print them in?

    [EDIT]
    Oh yeah, and don't #include <conio.h>. It's an outdated header that is Windows-specific, and you aren't actually using any features from that header anyway, so it's pointless to have it in there.
    [/EDIT]
    Last edited by anduril462; 04-18-2014 at 08:55 PM.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    By columns, do you mean like columns of an array, or just that each number needs to be printed on a different line? Also, is the output supposed to ignore the first number (the 9), or was that a typo?
    Last edited by Alpo; 04-18-2014 at 10:39 PM. Reason: #9

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 04-05-2014, 09:23 PM
  2. Replies: 6
    Last Post: 02-12-2013, 08:04 PM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM