The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Entering filename from keyboard.
Discuss Entering filename from keyboard. in the C Programming forum on Dev Shed. Entering filename from keyboard. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 19th, 2012, 08:31 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Location: Bristol
Posts: 13
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.
|

September 19th, 2012, 09:24 AM
|
|
|
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");
/* ... */
}
|

September 20th, 2012, 06:07 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Location: Bristol
Posts: 13
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. 
|

September 20th, 2012, 10:25 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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?
|

September 20th, 2012, 11:29 AM
|
|
|
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 |
|
|
|
fgets(filename, 1000, stdin); if (filename[strlen(filename) - 1] == '\n') 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!
|

September 20th, 2012, 02:48 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Location: Bristol
Posts: 13
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|