Thread: Porblems getting a program to find a file

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    15

    Porblems getting a program to find a file

    Hi all

    I am in the initial stages of writing a program to perform mathematical tasks from digits placed in a file, namley notepad. The program keeps returning "No such directory" when I enter a pre prepared file name and after several hours of going round in circles I am getting nowhere. The notepad file and the program are in the same folder (If this helps) and entering either just the file name or even the whole location string just returns the above error. Probably missing something really silly as I am quite new to this, does anyone know why I cant locate the file.

    Thanks

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    main(void)
    {
    char ch, filename[25];
    FILE *fp;
    printf("Enter the name of file you wish to see");
    gets(filename);
    
    fp = fopen(filename,"r");
    if(fp==NULL)
    {
     perror("Error.\n");
     exit(EXIT_FAILURE);
     }
     printf("The contents of %s file are :-\n\n", filename);
     
     while((ch=fgetc(fp) )!=EOF)
     printf("%c",ch);
     fclose(fp);
    return 0;
    
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try giving filename more space and entering the entire path again.

    I'm contractually obligated to also mention:
    • you should use fgets instead of gets (but beware that it leaves the newline in the string)
    • you should explicitly define main as returning an int
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    Hello

    Sorry should have seen the int before main, begining to wonder if i'll ever get the hang of this. I have tried giving the filename up to 100 characters but still returns the same error also not sure how to get fgets to work as when i enter it, it wont compile probably missing something there too lol.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Using fgets is more trouble but it allows you to protect your program from buffer overflow.
    Code:
    const int MAXSIZE = 100;
    char filename[MAXSIZE];
    int len;
    fgets(filename, MAXSIZE, stdin);
    len = strlen(filename);
    if (filename[len-1] == '\n')
        filename[--len] = 0;
    Can you post exactly what you're typing in for the full path?

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    The file is located in the following position, is not protected and is created by wordpad

    E:\Numbers

    Will give fgets another go

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Is Numbers a directory? If not, shouldn't it be Numbers.txt? Try opening wordpad or notepad and typing in the exact full path that you're typing into your program. Remember to include the drive letter.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    It was the txt that was missing on the end of the path, also got fgets to work will have to go over it later to try and make sense of it. Thankyou for you help.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Mystery solved!

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char ch, filename[25];
    ...
    while((ch=fgetc(fp) )!=EOF)
    The fgetc function returns an int, not a char. This is important when comparing against EOF.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with find prime program
    By Rukris in forum C Programming
    Replies: 5
    Last Post: 11-15-2011, 01:51 PM
  2. can't find error(s) in my program..help?
    By jennyt in forum C Programming
    Replies: 1
    Last Post: 11-23-2010, 08:11 PM
  3. Program does not find any matching file
    By tilex in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2005, 09:59 PM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  5. porblems with udp client
    By rotis23 in forum Linux Programming
    Replies: 0
    Last Post: 12-11-2002, 10:20 AM