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

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 19th, 2012, 08:31 AM
x.sophie.x x.sophie.x is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Location: Bristol
Posts: 13 x.sophie.x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 32 sec
Reputation Power: 0
Entering filename from keyboard.

Okay, so my project is pretty much finished. I need to put some notes throughout but otherwise there is only one sticking point.

The assignment calls for the user to be able to input the file name from keyboard.

I'll pop my coding in underneath:

Code:
int main()
{
    FILE *file_ptr;
    double x,y,sigmax,sigmay, myarray[10000][4],a[10000],b[10000],c[10000],d[10000],e[10000],value1,value2,value3,value4,value5,coeff1,coeff2,err1,err2;
    int i;

    file_ptr=fopen("C:\\Data\\LinearTestData.txt","r"); //open file from computer
    if (file_ptr == NULL) printf("Error: File not found.\a\n");
    else


then the code continues on. Putting the numbers in the file into a array and then doing some maths on them essentially.

I want to be able to ask the user, at the start of the code, to choose which file they want to read the data from. Do I use a string, a gets?

Thanks in advance,

Sophie.

Reply With Quote
  #2  
Old September 19th, 2012, 09:24 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
No! You do not use that function. That function is dangerous: it cannot be executed safely and there is a perfectly reasonable alternative: fgets(). Just remember to remove the trailing '\n' from the buffer before calling fopen.

You can also, maybe (depends on the requirements), use parameters to the program. In this way the user would pass the file to open as a command line parameter
Code:
int main(int argc, char **argv)
{
    /* ... */
    if (argc == 2) fopen(argv[1], "r");
    /* ... */
}

Reply With Quote
  #3  
Old September 20th, 2012, 06:07 AM
x.sophie.x x.sophie.x is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Location: Bristol
Posts: 13 x.sophie.x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 32 sec
Reputation Power: 0
Okay, had a go with the gets but it's not working at the moment. Not sure what to put into the file pointer part?

Here's the code:

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

int main()
{
    FILE *file_ptr;
    double x,y,sigmax,sigmay, myarray[20000][4],a[20000],b[20000],c[20000],d[20000],e[20000],value1,value2,value3,value4,value5,coeff1,coeff2,err1,err2;
    char Y, N, char1, char2, filename[1000];
    int i,j;

    Y='y';
    N='n';

    printf("Please input the address of your data file, using double backslashes, and incuding the file extension.\n");
    fgets(filename, 1000, stdin);

    file_ptr=fopen("?","r"); //open file from computer
    if (file_ptr == NULL) printf("Error: File not found.\a\n");
    else


Anyone? It's literally the last piece of my project.

Reply With Quote
  #4  
Old September 20th, 2012, 10:25 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 18 sec
Reputation Power: 1974
Huh? You read in the filename into the variable, filename. You then want to open that file by that filename. So you pass filename to fopen.

How could there possibly be any question or confusion about something as boneheaded simple as that?

Reply With Quote
  #5  
Old September 20th, 2012, 11:29 AM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,476 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 6 h 23 m 5 sec
Reputation Power: 2194
Eh, I bet with all those big stack-allocated arrays it's going to blow up at runtime anyway.

fgets includes the newline character. You need to remove it.

c Code:
Original - c Code
  1. fgets(filename, 1000, stdin);
  2. if (filename[strlen(filename) - 1] == '\n')
  3.   filename[strlen(filename) - 1] = '\0';
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

Reply With Quote
  #6  
Old September 20th, 2012, 02:48 PM
x.sophie.x x.sophie.x is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Location: Bristol
Posts: 13 x.sophie.x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 32 sec
Reputation Power: 0
@dwise1_aol

Rude

It runs it works fine, it was the new line that needed testing and removing. I figured, seeing as it didn't work as I expected, maybe it needed something different.

Seriously though, no need to be nasty.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Entering filename from keyboard.

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 - 2013, Jelsoft Enterprises Ltd.

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