Thread: Basic Char Array Issue

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    Basic Char Array Issue

    I am doing an assignment for class where the user is prompted for a file name, the program then does stuff to the information in the file, and then prints out a graph to the console.

    The issue I am getting is reading in the char array. Here is my current code for opening the file:
    Code:
    //Create file, in for input
         FILE *in;
         //Create char array for file name
         char fileName[50];
         //Prompts user for file name input
         printf("Please type the name of the file: ");
         //Get file name
         scanf("%c",fileName);
         
         //Debugging Here
         printf("File Name: '%c'\n",fileName);
         
         //Open input file
         in = fopen(fileName,"r");
    The problem I am having is that the char array doesnt seem to be reading in like intended. Can anyone help me out with how to correct read in an array of chars? I am receiving this character when I print out the fileName array in the debugging printout: ≡

    Any thoughts? The project is due midnight tonight (12 hours), so any swift help is appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    9
    Naturally I figured it out seconds after posting. Should have used %s instead of %c when reading in the char array.

    Should I do anything specific to close my question or how does that work on this forum?

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    AFAIK there's no "solved" status here, since it's a "discussion" forum and not a request/support place, so just leave it as it is. Maybe somone else wondering about the same thing finds help in it :-)

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also may I recommend against using scanf as much as possible. Look at fgets() instead, especially for reading in strings.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Should have used %s instead of %c when reading in the char array.
    Yes, but I'd recommend going even further than that. An unadorned %s in scanf is no better than gets when it comes to buffer overflow prevention. At the very least you should provide a field width to ensure that scanf doesn't keep writing to array elements that don't exist:
    Code:
    char fileName[50];
    scanf("%49s", fileName); /* 49 instead of 50 to allow for '\0' */
    Now the functionality is on par with fgets, which is the usual recommendation for string input:
    Code:
    char fileName[50];
    fgets(fileName, sizeof fileName, stdin);
    I won't overwhelm you with too much right now, but also keep in mind that error handling is very important. Both scanf and fgets return a value that can be used to check for success or failure. Likewise, when opening files, make sure the pointer returned is not NULL.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't beat the advice from claudiu and Prelude, for your input, but check out:
    File input print graph
    Last edited by Adak; 10-27-2010 at 03:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with Linked Listing
    By DKING89 in forum C Programming
    Replies: 1
    Last Post: 04-07-2008, 10:12 AM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM