
December 1st, 2012, 08:55 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 21 m 41 sec
Reputation Power: 0
|
|
|
Obtaining file name from user with getline
I am having a problem getting this code to work. I need to obtain a file name from a user from the keyboard, open that file and read the data. The assumption is the user will enter the complete name including extension.
I have checked my code and I know the string the user inserts is being saved right, but fopen returns null even for valid files. so it seg faults at every run.
I just can't figure out what I have overlooked. The file lines are exact code my instructor provided, all the examples online are very similar. A little help would be great.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
int filenm = 100;
char * filename;
FILE * input;
double value;
printf("\nPlease enter the complete name of your data file: ");
getline(&filename, &filenm, stdin);
printf("The file name is %s", filename);
if (filename != NULL)
input = fopen(filename, "r");
fscanf(input, "%lf", value);
printf("\nThe value is %lf", value);
fclose(input);
return 0; }
|