C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 1st, 2012, 09:55 AM
slingersjilly slingersjilly is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 slingersjilly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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; }

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Obtaining file name from user with getline


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap