0

I can't seem to find an answer to the fopen issue. It seems to be common, but everything I'm trying is not working, from what I have read this seems to be caused by a trailing newline, but I'm not sure:

The aim is to move from the hardcoded filename (which is commented) to the command line input for the file name

any help would be much appreciated

My code

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

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

    setbuf(stdout, NULL);

    struct Person { char lname[20]; char fname[20]; int id; };
    int N1, i ;
    struct Person *p;


//    char filename[] = "../src/students.csv" ;
//
//    FILE *str = fopen( filename, "r") ;
//    if (str==NULL) {
//        perror(filename) ;
//        exit(-1) ;
//    }


    printf("Please input file name:"); // Asks user to enter file name.
    FILE *str2 = fopen(argv[1], " r"); // File opened for reading
    printf("%p", str2);
    if (str2 == NULL) { // If issue opening file display the error.
        perror(argv[1]);
        exit(-1);

    }

    fscanf( str2, "%d\n", &N1 ) ;

    p = (struct Person*)calloc( N1 , sizeof(struct Person) ) ;
    if (p==NULL) {
        fprintf(stderr, "Can't allocate %d structs -- exiting.\n", N1 ) ;
        exit(-1) ;
    }

    for ( i=0 ; i<N1 ; i++ ) {
        fscanf( str2, " %[^,],%[^,],B%d", p[i].lname, p[i].fname, &(p[i].id) );
    }

    fclose(str2) ;

    /* Access the array... Just like a static array */
    printf("%s, %s, B000%d\n", p[4].lname, p[4].fname, p[4].id) ;

    free(p) ;

    return 0;
}

studnets.csv

bob  hope    B0011001
jim real    B0011002
dean    egan    B0011003
mary    Sherri  B0011004
harry   full    B0011005
peg max B0011006
sun lee B0011007
bill    bob B0011008

user3292394
  • 609
  • 2
  • 11
  • 24
  • 1
    `I can't seem to find an answer to the fopen issue.` _Which_ fopen issue? – tkausl Dec 30 '20 at 15:12
  • 3
    "`printf("Please input file name:"); // Asks user to enter file name.`" - but you never ask the user to enter the file name. You use `argv[1]`. – Ted Lyngmo Dec 30 '20 at 15:13
  • 1
    *Never* access `argv` without first checking `argc`. – Weather Vane Dec 30 '20 at 15:18
  • It is poor practice to include the `'\n'` newline in the `fscanf` format string, and to ignore the value returned by the function. Instead I suggest adding a space to `fscanf( str2, "%[^,] ...` so it is `fscanf( str2, " %[^,] ... ` and remove the newline from all the `fscanf` format strings. – Weather Vane Dec 30 '20 at 15:20
  • Sorry I am very new to C, @TedLyngmo does ```argv[1]``` not tell the program to use to first arg passed in? @WeatherVane I will reformat the `fscan` – user3292394 Dec 30 '20 at 15:35
  • Yes, `argv[1]` will be the first argument passed to the program at start up (if any argument was passed to the program - you must always check that). But your comment and the `printf` suggests that the user is supposed to enter the filename interactively, when the program has already started - and that's not happening. – Ted Lyngmo Dec 30 '20 at 15:39
  • @TedLyngmo the intent is to get the user to type the name of the file. to achieve this do I have to use `scanf` ? – user3292394 Dec 30 '20 at 15:41
  • Sure, `scanf` is one to let the user enter a string. `fgets` is another. – Ted Lyngmo Dec 30 '20 at 15:46
  • @TedLyngmo, is the `fscanf( str2, "%d\n", &N1 ) ;` not used to read the file from the command line ? – user3292394 Dec 30 '20 at 15:54
  • Perhsps that'd be best suited for a separate quetion if the answer can't be found among the existing answers here at SO. – Ted Lyngmo Dec 30 '20 at 17:20

0 Answers0