Thread: opening a user selected file

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Question opening a user selected file

    i'm learning how to use files in C...

    now, when i want to open user selected file, i can write something like this:
    this program opens a user selected file and prints it out on the screen.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    main()
    {
    	char input[50];
    	FILE* f1;
    	char out;
    	while(1)
    	{
    		printf("enter filename (up to 50 chars): ");
    		gets(input);
    		f1=fopen(input,"r");
    		printf("\n");
    		if(f1)
    		{	
    			while ((out = fgetc(f1)) != EOF)
    			    printf("%c",out);
    			printf("\n------------------------------------------------------------\n");
    		}
    	}
    }
    the problem which this is that the user has to input 'filename.txt' instead of just 'filename'

    i thought of just adding .txt to the string manually and then calling fopen but that seems like the dumb way to go about it.
    .
    can i somehow add the .txt to the argument without adding it to the string ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Would it be so bad to add a strcat?
    But you have more severe problems to fix, as well.
    Don't use gets (click me).
    Don't use implicit main (click me).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    10
    no, it wouldn't be so bad...
    i was just wantering if i could somhow add the format to the argument without adding it to the string.
    .
    maybe something like:

    f1=fopen("%s.txt",input,"r");

    when input is the formatless filename.
    this syntax doesn't work, or i wouldn't have to ask.
    but which syntax does ?
    can it even be done ?
    .
    i'm writing this for a university course...
    usually i wouldn't use an unsafe function like gets(), but since i am to assume all input is valid, i don't really see the point.
    .
    as for the implicit main...
    i heard that's a problem, but that's how i learned and it doesn't seem to cause any trouble.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    87
    i thought of just adding .txt to the string manually and then calling fopen but that seems like the dumb way to go about it.
    .
    can i somehow add the .txt to the argument without adding it to the string ?
    How else would you do it? There is nothing built into any of the standard libraries that magically knows you want to append ".txt" onto your filename.

    Also, read the manual on fgetc, in particular paying attention to the return type/value.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by jason_m View Post
    How else would you do it? There is nothing built into any of the standard libraries that magically knows you want to append ".txt" onto your filename.

    not magically, no.
    but look at this for example:
    say we have a function that accepts an int and returns another int.

    Code:
    {
    int x, j=0;
    
    x=somefunction(j+1);
    }
    in this case the value of the variable j is 0 but the value of the argument is 1.

    i changed the argument without changing the variable it's based on.

    can something similar be done with strings ?

    something like this, but in a different syntax, since this doesn't work:

    Code:
    input="filename";
    
    f1=fopen("%s.txt",input,"r");
    Last edited by SymDePro; 07-12-2009 at 05:07 PM.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    First of all please make sure that you follow what the forum members are trying to say to you.

    i'm writing this for a university course...
    usually i wouldn't use an unsafe function like gets(), but since i am to assume all input is valid, i don't really see the point.
    .
    as for the implicit main...
    i heard that's a problem, but that's how i learned and it doesn't seem to cause any trouble.
    main is not supposed to be used like that and so is the gets() . There are other functions which can be used like scanf. And as was pointed out earlier make use of strcat() which adds two strings.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    	char input[50];
    	FILE* f1;
    	char out;
    	while(1)
    	{
    		printf("enter filename (up to 50 chars): ");
    		//gets(input);
    		scanf("%s", input);
    		strcat(input,".txt");
    		f1=fopen(input,"r");
    		printf("\n");
    		if(f1)
    		{	
    			while ((out = fgetc(f1)) != EOF)
    			    printf("%c",out);
    			printf("\n------------------------------------------------------------\n");
    		}
    	}
    	return 0;
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then I'll add yet another link: Scanf woes
    Scanf isn't completely safe either.
    OP: You really should read the buffer overrun section. Just because you think it's safe is no excuse. Buffer overruns can cause horrible stuff.
    And for the question, I know of no way of doing it in C. You can always copy the string into another variable and append .txt, but otherwise, no...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Thanks @above,

    Now i think i ll make sure that i give the buffer size when i use scanf till the time i become comfortable with using fgets.

    PS:
    The program above which i used can be given with just the filename without appending the .txt . It works as in the next line i call strcat to append .txt to the filename before actually opening the file.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Looks like you want to use formatting convention to fill out the file name. Something like:
    Code:
    char temp_buf[300];
    ...
    sprintf(temp_buff, "%s.txt", input); 
    f1=fopen(temp_buf, "r");

  10. #10
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by nonoob View Post
    Looks like you want to use formatting convention to fill out the file name. Something like:
    Code:
    char temp_buf[300];
    ...
    sprintf(temp_buff, "%s.txt", input); 
    f1=fopen(temp_buf, "r");
    Just to be safe check that sprintf returns a values <= 300. Or make use of snprintf.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Good point. I was relying on the max file/folder name length type things. But ya never know what a crazily flailing user might do when given an input prompt. Those darn pesky users!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM