file handling functions

This is a discussion on file handling functions within the C Programming forums, part of the General Programming Boards category; When I try to run this program it says "File doesn't exist" but the file dose exist under C:\ can ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    file handling functions

    When I try to run this program it says "File doesn't exist" but the file dose exist under C:\ can someone help please.

    Code:
      #include "stdio.h"
       main( )
       {
         FILE *text;
         char c;
         text = fopen("C:\text.txt", "r");
         if (text == NULL) printf("File doesn't exist\n");
         else {
          do {
           c = getc(text); /* get one character from the file
           */
             putchar(c); /* display it on the monitor
           */
           } while (c != EOF); /* repeat until EOF (end of file)
         */
         }
        fclose(text);
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    Not:
    "C:\text.txt"
    Use:
    "C:\\text.txt"
    backslash is the escape character, and starts escape sequences. \t, specifically, is a tab. You're trying to open "C: ext.txt"

    Also, your end-of-file detection won't work correctly. If getc() returns EOF, you first print it to the screen, and then check if you've hit the end of the file. Reverse it: Check first if getc() has returned EOF, then print to screen.
    Last edited by Cactus_Hugger; 10-18-2007 at 10:16 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Quote Originally Posted by Cactus_Hugger View Post
    Code:
    Not:
    "C:\text.txt"
    Use:
    "C:\\text.txt"
    backslash is the escape character, and starts escape sequences. \t, specifically, is a tab. You're trying to open "C: ext.txt"
    Ah ha ok thanks Cactus_Hugger that fixed it

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    How would I make the below code so the file is found and printed out on the monitor by user input.

    Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
       main( )
       {
         FILE *text;
         char c;
         printf("Please enter file name to open:\n");
         text = fopen("C:\\text.txt", "r");
         if (text == NULL) printf("File doesn't exist\n");
         else {
          do {
           c = getc(text); /* get one character from the file
           */
             putchar(c); /* display it on the monitor
           */
           } while (c != EOF); /* repeat until EOF (end of file)
         */
         }
    
        fclose(text);
        system("PAUSE");
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    add something like:
    Code:
    #include <stdlib.h>
    ...
    int main(...)
    {
    ...
        char fname[MAX_PATH];
    ...
        fgets(fname, sizeof(fname), stdin);
    Now replace your constant with fname, and all should work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Quote Originally Posted by matsp View Post
    add something like:
    Code:
    #include <stdlib.h>
    ...
    int main(...)
    {
    ...
        char fname[MAX_PATH];
    ...
        fgets(fname, sizeof(fname), stdin);
    Now replace your constant with fname, and all should work.

    --
    Mats
    Hey matsp ok thanks again I will give it a go

    Cheers

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    OK I this code and get the following errors

    Code:
    12 c:\mjm7ss~1.cpp incompatible types in assignment of `int' to `char[260]'
    14 c:\mjm7ss~1.cpp passing `char *' to argument 1 of `putchar(int)' lacks a cast
    16 c:\mjm7ss~1.cpp ANSI C++ forbids comparison between pointer and integer
    Code:
    #include <stdio.h>
    #include <stdlib.h>
       main( )
       {
         FILE *text;
         char fname[MAX_PATH];
         printf("Please enter file name to open:\n");
         fgets(fname, sizeof(fname), stdin);
         if (text == NULL) printf("File doesn't exist\n");
         else {
          do {
           fname = getc(text); /* get one character from the file
           */
             putchar(fname); /* display it on the monitor
           */
           } while (fname != EOF); /* repeat until EOF (end of file)
         */
         }
    
        fclose(text);
        system("PAUSE");
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Nah, that's wrong. You want to use fname as an argument to fopen().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Is this getting close LOL

    Code:
    #include <stdio.h>
    #include <stdlib.h>
       main(char *fname[] )
       {
         FILE *text;
         char c;
         char fname[MAX_PATH];
         printf("Please enter file name to open:\n");
         fgets(fname, sizeof(fname), stdin);
         if (text == NULL) printf("File doesn't exist\n");
         else {
          do {
           c = getc(text); /* get one character from the file
           */
             putchar(c); /* display it on the monitor
           */
           } while (c != EOF); /* repeat until EOF (end of file)
         */
         }
    
        fclose(text);
        system("PAUSE");
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to get this line:
    Code:
         text = fopen("C:\\text.txt", "r");
    back in, but replace your "C:\\text.txt" with fname.

    [I am being kind today, am I not :-)]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Quote Originally Posted by matsp View Post
    You need to get this line:
    Code:
         text = fopen("C:\\text.txt", "r");
    back in, but replace your "C:\\text.txt" with fname.

    [I am being kind today, am I not :-)]

    --
    Mats
    Yes and very patient ok so now i have this but am getting error '7 c:\mjm7ss~1.cpp
    declaration of `fname' shadows a parameter'

    Code:
    #include <stdio.h>
    #include <stdlib.h>
       main(char *fname[] )
       {
         FILE *text;
         char c;
         char fname[MAX_PATH];
         printf("Please enter file name to open:\n");
         text = fopen("fname", "r");
         fgets(fname, sizeof(fname), stdin);
         if (text == NULL) printf("File doesn't exist\n");
         else {
          do {
           c = getc(text); /* get one character from the file
           */
             putchar(c); /* display it on the monitor
           */
           } while (c != EOF); /* repeat until EOF (end of file)
         */
         }
    
        fclose(text);
        system("PAUSE");
    }

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Close but no cigar:
    First of all:
    Code:
       main(char *fname[] )
    What the heck is fname doing here? Remove it, leave just empty () like it was in the first few posts.

    Are you intending to use command-line arguments? That's not the problem I'm trying to get to towards. And your prompt "Please enter filename" isn't indicating that sort of use.

    Second problem is that you have quotes around fname - that means that you are trying to open a file called "fname", not called whatever is in the variable fname that the user entered in the fgets() statement.

    And finally, you try to open the file before you've read it's name, so that won't work. Swap some lines about [try to think about what does what, and not just randomly swap lines around - that won't work unless you are lucky, and if you're that lucky, buy a lottery ticket, don't try programming! :-)]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    I am starting to go nuts... Maybe I should go buy a lottery ticket

    Code:
    #include <stdio.h>
    #include <stdlib.h>
       main( )
       {
         FILE *text;
         char c;
         char fname[MAX_PATH];
         printf("Please enter file name to open:\n");
         fgets(fname, sizeof(fname), stdin);
         text = fopen(fname, "r");
         if (text == NULL) printf("File doesn't exist\n");
         else {
          do {
           c = getc(text); /* get one character from the file
           */
             putchar(c); /* display it on the monitor
           */
           } while (c != EOF); /* repeat until EOF (end of file)
         */
         }
    
        fclose(text);
        system("PAUSE");
    }

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, yes, fgets() includes the newline in the line of text. If you add this between fgets() and fopen(), it should fix it up:
    Code:
    if (fname[strlen(fname)-1] == '\n') fname[strlen(fname)-1] = 0;
    Sorry about that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Quote Originally Posted by matsp View Post
    Sorry, yes, fgets() includes the newline in the line of text. If you add this between fgets() and fopen(), it should fix it up:
    Code:
    if (fname[strlen(fname)-1] == '\n') fname[strlen(fname)-1] = 0;
    Sorry about that.

    --
    Mats
    Hey No problem I am very grateful for all the help you guys have been giving me, It works now thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Mutlithreaded file handling
    By nvoigt in forum Windows Programming
    Replies: 11
    Last Post: 06-30-2005, 02:39 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  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