User-specified input file issue.

This is a discussion on User-specified input file issue. within the C Programming forums, part of the General Programming Boards category; Hi all, I've recently started programming at university and am having a bit of trouble with it. I've begun a ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    User-specified input file issue.

    Hi all, I've recently started programming at university and am having a bit of trouble with it. I've begun a piece of work that requires the user to enter a filename, read it into an array, do some maths/statistics and display a variety of results in tables. Annoyingly, I can't get the first part working - my attempt thus far is as follows;

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    int main(void)
    {
     	char filename[25];
    	int numbers;
    
    	FILE * fptr;
    	printf("Please enter a filename: ");
    	scanf(" %24s",filename[25]);
    	fptr=fopen(filename[25],"r");
     	if (fptr==NULL) printf("Open failed.");
     	while(fscanf(fptr," %d",&numbers)!=EOF)
    		printf(" %3d",numbers);
     	fclose(fptr);
    	 
        getchar();
        getchar();
        return 0;
    }
    The program seems to freeze as soon as I type in the filename and hit enter; I was under the impression that the scanf would read in whatever I typed to filename[25], so if I typed for example "statsdata.txt", this would then open the file as if I'd simply typed fptr=fopen("statsdata.txt","r"); normally, so I'm not sure what to do!

    Also, in my file I have 8 numbers seperated by whitespace; if I type out the fptr=fopen("statsdata.txt","r"), these numbers are displayed onscreen. I'm unsure though if these are now in an array or not - is filename[25] here considered to be a string and unrelated to the numbers within?

    The program required is pretty complex for my current knowledge, kind of stressing about it which doesn't help either. Thanks a lot for any help you can give.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    int main(void)
    {
     	char filename[25];
    	int numbers;
    
    	FILE * fptr;
    	printf("Please enter a filename: ");
    	scanf(" %24s",filename[25]);
    	fptr=fopen(filename[25],"r");
     	if (fptr==NULL) printf("Open failed.");
     	while(fscanf(fptr," %d",&numbers)!=EOF)
    		printf(" %3d",numbers);
     	fclose(fptr);
    	 
        getchar();
        getchar();
        return 0;
    }
    To start, the bits in red should be eliminated.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    Ah, I see, that's sorted it out. Seems such a basic mistake..going to have to read over my notes again (and again) and improve general c knowledge I think!

    Thanks again, hopefully can make some progress with the rest of it now.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Yep - in that case, you want just the name of the char array, filename. When you use the just the name, it is the same as doing filename[0]. What you had done was pass it an address to the end of the array, which is usually not good.

    Sometimes it is good to just test things on a smaller scale. Just work on the part to do with getting the user input, and then print it out. If you get that working, then you can use the name of the file to open the file with fopen.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,287
    Quote Originally Posted by kermit View Post
    Yep - in that case, you want just the name of the char array, filename. When you use the just the name, it is the same as doing filename[0].
    To be clear, it's the same as doing &filename[0]. filename[0] is just a single character, &filename[0] or just filename refer to the address of the first element, which is how you refer to "strings" in C.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by anduril462 View Post
    To be clear, it's the same as doing &filename[0]. filename[0] is just a single character, &filename[0] or just filename refer to the address of the first element, which is how you refer to "strings" in C.
    Indeed. Good catch on a sloppy answer from me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-28-2010, 10:21 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21