Thread: unable to access the file

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    228

    unable to access the file

    This is how my c program looks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXFILELENGTH 20
    #define BUFFERSIZE 1000
    #define IN 1 // This means that we are inside a comment.
    #define OUT 0 // This means that we are outside a comment.
    
    void readSourceFile(FILE *, char[], int sizeOfFile);
    
    int main()
    {
        FILE *sourceFile = NULL, *outputFile = NULL; // The sourceFile is the c text file with the comments to be removed.
        char srcFileName[MAXFILELENGTH], outFileName[MAXFILELENGTH], srcData[BUFFERSIZE]; // data contains all the characters retrieved from the source file.
    
        printf("What is the name or path of the source file that you're trying to access?\n");
        fgets(srcFileName, MAXFILELENGTH, stdin);
        printf("\nWhat is the name or path of this output file?\n");
        fgets(outFileName, MAXFILELENGTH, stdin);
    
        sourceFile = fopen(srcFileName, "r");
        outputFile = fopen(outFileName, "w");
    
        if(sourceFile == NULL)
        {
            printf("\nThe name of the source file does not exist!\n");
            return EXIT_FAILURE;
        }
        
        readSourceFile(sourceFile, srcData, BUFFERSIZE);
    
        fclose(sourceFile);
        fclose(outputFile);
    
        return 0;
    }
    
    void readSourceFile(FILE *filePtr, char srcData[], int sizeOfFile)
    {
        while(fgets(srcData, sizeOfFile, filePtr) != NULL) // Once we reach the end of the file we stop reading data.    
        {
    
    
        }
    }
    The file that I'm working with is a text file called main.txt and it is placed right next to my main.c(the main.txt is from a another program so they are completely unrelated). I am certain that the reason why it doesn't work is because of the newline that gets appended when using the fgets function. I enter the string literal "main.txt" and the program ran successfully without the new line. So what can I do to remedy this situation?
    Last edited by deathslice; 10-23-2015 at 05:44 PM.

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Check your return values!
    Code:
        if (fgets(srcFileName, sizeof srcFileName, stdin) == NULL) {
            /* End of input! */
            return EXIT_FAILURE;
        }
    
        /* Remove the newline (and anything following it,
         * except it is never followed by anything in this particular case).
        */
        srcFileName[strcspn(srcFileName, "\n")] = '\0';
    
        if (srcFileName[0] == '\0') {
            /* Line was empty! */
            return EXIT_FAILURE;
        }
    The strcspn() function is declared in <string.h>, and reports the index of the first character in the specified list (we only listed newline, \n). If the string does not contain any of those, it returns the length of the string. For example, to remove everything following the first digit in string foo, including the digit, you can use
    foo[strcspn(foo, "0123456789")] = '\0';

    Second, if you include <string.h> and <errno.h>, you can check and report the reason for the file open failure:
    Code:
        sourceFile = fopen(srcFileName, "r");
        if (sourceFile == NULL) {
            fprintf(stderr, "%s: %s.\n", srcFileName, strerror(errno));
            return EXIT_FAILURE;
        }
    errno is a special variable declared by <errno.h>. Many C library functions set it to indicate the cause when a problem occurs. It is not zeroed by anything, unless you zero it out yourself (errno = 0; is perfectly legal).

    Note that because many functions set it when an error occurs, you cannot do other stuff in between an error and examining errno. You can copy it into an int variable, however. For example, if you wanted to close the sourceFile if opening the output file fails, you could do it thus:
    Code:
        outputFile = fopen(outFileName, "w");
        if (outputFile == NULL) {
            const int errnum = errno;
            fclose(sourceFile);
            fprintf(stderr, "%s: %s.\n", srcFileName, strerror(errnum));
            return EXIT_FAILURE;
        }
    I used const int errnum = errno; instead of int errnum; errnum = errno; above, because errnum is not going to be modified.

    (fclose() is one of those functions that is allowed to set errno if fclose() fails. Its failure never means the file handle remains open; it just means an error occurred when the file was closed. If it occurs when closing a file you wrote to, you should treat it as a write error. Most programmers do not bother, though; I hate that.)

    It is a good practice to sprinkle const whenever the variable is not supposed to be modified, and also when the pointer target is only to be read, not written to. It helps the compiler check things, and occasionally even generate better code; but most of all it is helpful to other programmers, because they can see which stuff is supposed to be mutable, and which stuff constant. Helps understand what the original programmer intended, you see.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    228
    Thanks for the reply it really helped.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is C, not C++. You'll get better answers if you put your topics in the C section in the future.
    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.

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    228
    Yes I realized a couple of seconds too late right after I created this thread. Once I did, I didn't want to create a double post of this. So if moderators decide to move this thread to the c forum, that's fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to create file
    By juice in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2012, 09:24 AM
  2. Unable to open file using fopen()
    By sanddune008 in forum C Programming
    Replies: 7
    Last Post: 01-13-2012, 10:46 AM
  3. Unable to read from a file
    By Cnewbi in forum C Programming
    Replies: 7
    Last Post: 01-06-2012, 08:12 AM
  4. unable to access structure within a function
    By bluetxxth in forum C Programming
    Replies: 7
    Last Post: 02-17-2010, 03:52 AM
  5. unable to write to a file
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 08:18 AM