Jacob Sprague
Jacob Sprague

Reputation: 1

Read in text file with multiple elements per line

I need to read in a text file, "input.in", so that I can run a sort function on the code, according to id. The input.in file contains an id and name of file, 8 lines total. I know that I need to read in the input file line by line (not sure if my code is correct). But the main problem is that the fopen function is returning the result that it can't find the input file, even though its on the desktop along with the source file being saved there.

Any tips would be greatly appreciated

int main()
{
int id;
char node;
char item[9], status;

FILE *fp;

if((fp = fopen("/Users/jacobsprague/Desktop/input.txt", "r+")) == NULL)
{
    printf("No such file\n");
    exit(1);
}

while(42)
{
    int ret = fscanf(fp, "%s %c", id, &node);
    if(ret == 2)
        printf("\n%s \t %c", id, node);
    else if(errno != 0)
    {
        perror("scanf:");
        break;
    }
    else if(ret == EOF)
    {
        break;
    }
    else
    {
        printf("No match.\n");
    }
}
printf("\n");
if(feof(fp))
{
    puts("EOF");
}

return 0;
}

Here is the input file contents:

8
4 Node1111
8 Node11111111
2 Node11
7 Node1111111
1 Node1
5 Node11111
6 Node111111
3 Node111

Upvotes: 0

Views: 1015

Answers (2)

user3629249
user3629249

Reputation: 16540

// 1) there were lots of little oops in the ops code,
// 2) the op skipped the detail that the first line contains
//    a count of the number of following lines
// all of that is corrected in the following

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

int main()
{
    int  id;        // value read from file
    char node[30];  // string read from file
    //char item[9]; // if not commented, raises compiler warning about unused variable
    //char status;  // if not commented, raises compiler warning about unused variable
    int  ret;       // returned value from fscanf
    int  lineCount = 0; // number of lines in file after first line
    int  i;         // loop counter

    FILE *fp;
    if((fp = fopen("/Users/jacobsprague/Desktop/input.in", "r")) == NULL)
    {
        // perror also outputs the value of errno and the results of strerror()
        perror( "fopen failed for file: input.in");
        exit(1);
    }

    // implied else, fopen successful

    // get first line, which contains count of following lines
    if( 1 != (ret = fscanf(fp, " %d", &lineCount)) )
    { // fscanf failed
        perror( "fscanf");  // this also outputs the value of errno and the results of strerror()
        exit( EXIT_FAILURE );
    }

    // implied else, fscanf successful for lineCount

    for( i=0; i < lineCount; i++) // read the data lines
    {
        // note leading space in format string to consume white space (like newline)
        if( 2 != (ret = fscanf(fp, " %d %s", &id, node)) )
        { // fscanf failed
            // this also outputs the value of errno and the results of strerror()
            perror( "fscanf for id and node failed");
            break;
        }

        // implied else, fscanf successful for id and node

        printf("\n%d\t %s", id, node);
    } // end for

    printf("\n");
    if( EOF == ret )
    {
        puts("EOF");
    } // endif

    return 0;
}  // end function: main

Upvotes: 0

seeseac
seeseac

Reputation: 151

fopen can fail for reasons other than not finding the file, so you should check errno to see what the problem was. However in this case, as BLUEPIXY has mentioned, the problem appears to be that you have typed input.txt instead of input.in.

Upvotes: 1

Related Questions