C- programming: Reading filename from keyboard

In summary, you are trying to write a program that will open a file by entering its name from the keyboard, but it is not working. Gets, a function in string that allows for string overrun exploits, is a bad idea for your own code, but it works in this case.
  • #1
malawi_glenn
Science Advisor
Homework Helper
6,735
2,458
[SOLVED] C- programming: Reading filename from keyboard

Hi!

I am trying to write a small program for open a file by entering filename with keyboard, but it does not work. Why?


int main(){


FILE *fp;


printf("Give filename ");

char str[100], *s;

s = fgets(str, 100 , stdin);

printf("\n");

fp = fopen(str, "r");

-----------

No file opens.. :(
 
Technology news on Phys.org
  • #2
try:
Code:
#include <stdio.h>
#include <string.h>

int main()
{
   FILE *fp=NULL;;
   char str[100]={0x0};
   char *s=NULL;;

   printf("Give filename ");
   s=fgets(str, 100 , stdin);
   s=strchr(str,'\n');
   if(s!=NULL) *s=0x0;
   fp = fopen(str, "r");
   printf("%s open = ", str);
   if(fp!=NULL)
     printf("success\n");
   else
     printf("failure");
   fclose(fp);
   return 0;
}
 
  • #3
I tried:

printf("Give filename ");

gets(filename);


fp = fopen(filename, "r");

-------

and it worked.
 
  • #4
fgets returns the trailing \n which is not part of the filename.
 
  • #5
jim mcnamara said:
fgets returns the trailing \n which is not part of the filename.

ok? but it worked
 
  • #6
Oh - in general, gets is a BAD idea - it allows string overrun exploits. For your own code it is fine doing desktop programming.
 
  • #7
gets and fgets are not the same thing.
 
  • #8
what is desktop programming?
 
  • #9
that fgets and gets is not the same thing i know :-)
 
  • #10
desktop programming - writing code for personal use in your profession. Also called 'skunkworks' coding. So if you play with quantum gravity, you might write some quick and dirty Mathematica routines to do a quick analysis. You probably would not consider it suitable for a publication.
 
  • #11
yeah now I see. This is just for personal use, I want to learn C. Iknow Java very good, and want to do C++, but thinks is funnier to go to basics. Also we have a C-programming project in school, and I just thinks C is very cool. After this I will try Fortran.

thanx for your help!
 

FAQ: C- programming: Reading filename from keyboard

How do I read a filename from the keyboard in C programming?

To read a filename from the keyboard in C programming, you can use the scanf() function. This function takes two arguments - the format specifier %s to indicate that you want to read a string, and the variable name where you want to store the input. For example, if you have a variable filename, you can use scanf("%s", filename); to read a filename from the keyboard.

Can I use gets() to read a filename from the keyboard in C programming?

Yes, you can use the gets() function to read a filename from the keyboard in C programming. However, it is not recommended as it does not provide any protection against buffer overflow, which can lead to security vulnerabilities. It is better to use scanf() or fgets() instead.

How do I handle errors when reading a filename from the keyboard in C programming?

In order to handle errors when reading a filename from the keyboard in C programming, you can use the return value of the scanf() or fgets() function. These functions return the number of items successfully read, so you can check if the return value is less than the expected number of items and handle the error accordingly.

Can I use command line arguments to read a filename in C programming?

Yes, you can use command line arguments to read a filename in C programming. Command line arguments are passed to the main() function as parameters, and can be accessed using the argc and argv variables. The first command line argument, argv[0], is always the name of the program, and any additional arguments can be accessed using argv[1], argv[2], and so on.

How do I open and read a file using the filename I have read from the keyboard in C programming?

To open and read a file using the filename you have read from the keyboard in C programming, you can use the fopen() and fgets() functions. First, you need to open the file using fopen() by passing in the filename and the mode in which you want to open the file (e.g. "r" for read mode). Then, you can use fgets() to read the contents of the file line by line and store them in a variable. Don't forget to close the file using fclose() when you are done reading.

Back
Top