error opening the file...

This is a discussion on error opening the file... within the C Programming forums, part of the General Programming Boards category; I have this wherein i have to read from a file and then convert string to integer (implementation of atoi) ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    error opening the file...

    I have this wherein i have to read from a file and then convert string to integer (implementation of atoi)

    I try running it in my IDE visual c++ but it says error opening the file. Then i tried running it under cygwin but again nothing seems to happen. I cut copy pasted the path of the file from the properties of the file.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int myatoi(const char *);
    
    int main(void)
    {
    	char c[10];
    	int sum = 0;
    	FILE *file;
    
    	file = fopen("C:/Documents and Settings/ROHAN/Desktop/numbers.txt","r");
    	if(file == NULL)
    	{
    		printf("Error opening the file");
    		return 1;
    	}
    	else
    	{
    		printf("\nFile opened successfully");
    
    		while((fgets(c, 10, file)!= NULL))
    			sum += myatoi(c); // convert string to integer
    	}
    	printf("\n%d", sum);
    	printf("\nNow closing the file");
    	fclose(file);
    	printf("\nExiting");
    	return 0;
    }
    
    int myatoi(const char *str)
    {
    	int result = 0;
    
    	while(isdigit(*str))
    	{
    		result *= 10;
    		result += *str - '0';
    		printf("\n%c \t%d", *str, *str);
    		str++;
    	}
    	return result;
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Change the path to
    Code:
    file = fopen("C://Documents and Settings//ROHAN//Desktop//numbers.txt","r");
    single backslash to double backslash
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    No it doesnt help either its still the same error

    ERROR OPENING THE FILE

    I have a file by that name on the path that i have given here

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Check the actual path of the file, whether uppercase or lowercase etc.........
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Ok. The path is wrong. The slash is to be converted to a " double backslash".
    like this "\\"
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    @BEN10

    Its that it has to be forward slash always //

    This is what i found out
    Always use *forward* slashes for file names. The ANSI C standard allows forward slashes to be used in file names as the path separator, regardless of the separator used by the operating system.. This way, you don't have any problems porting your code from one OS to another.

    So in a C or C++ program, "/Dir1/Dir2/File3" is eqiuvalent to \Dir1\Dir2\File3 in Windows/DOS and /Dir1/Dir2/File3 in UNIX. This is one of the little known facts of file name handling.

    But it doesnt help either.

  7. #7
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by BEN10 View Post
    Ok. The path is wrong. The slash is to be converted to a " double backslash".
    like this "\\"
    That doesn't matter, '/' and '\\' are the same thing as C lets you use both so it should'nt make a difference.

    Well, I tried it on my compiler and it works fine. I was getting some strange results once in Vista due to user privileges. Maybe thats the problem ?

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Thanks both of you for the info. The program is running fine in my IDE also.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> The ANSI C standard allows forward slashes to be used in file names as the path separator, regardless of the separator used by the operating system.. This way, you don't have any problems porting your code from one OS to another.

    AFAIK, the standard doesn't have anything to do with it (except perhaps in include directives). As far as slashes embedded in text passed to file I/O functions, I think it's strictly dependant on the OS's treatment of the character. Incidentally, almost all operating systems recognize the forward slash, so it's usually the "safest" choice.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Are you sure the file exists? Check the variable errno (after including <errno.h> ) and see exactly what the trouble is. Most common problem is "Permission denied" after "No such file or directory".

  11. #11
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay i checked the errno and it is 2

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    
    int myatoi(const char *);
    
    int main(void)
    {
    	char c[10];
    	int sum = 0;
    	FILE *file;
    
    	file = fopen("C://Documents and Settings//ROHAN//Desktop//numbers.txt","r");
    	if(file == NULL)
    	{
    		printf("Error opening the file");
    		printf("\n%d", errno);
    		return errno;
    	}
    	else
    	{
    		printf("\nFile opened successfully");
    
    		while((fgets(c, 10, file)!= NULL))
    			sum += myatoi(c); // convert string to integer
    	}
    	printf("\n%d", sum);
    	printf("\nNow closing the file");
    	fclose(file);
    	printf("\nExiting");
    	return 0;
    }
    
    int myatoi(const char *str)
    {
    	int result = 0;
    
    	while(isdigit(*str))
    	{
    		result *= 10;
    		result += *str - '0';
    		printf("\n%c \t%d", *str, *str);
    		str++;
    	}
    	return result;
    }
    What does this imply?

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    OK, we're making progress here.

    If it's Microsoft, 2 is the constant ENOENT - which means it couldn't open the file for reason that it doesn't exist. You better recheck the exact spellings, number of spaces, etc. Best to cut-and-paste right from the Address bar when you're seeing the file.

    PLEASE use single forward slashes.
    "C:/Documents and Settings/ROHAN/Desktop/numbers.txt"
    Or double backslashes as in
    "C:\\Documents and Settings\\ROHAN\\Desktop\\numbers.txt"
    Last edited by nonoob; 07-27-2009 at 01:48 PM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Also note that "/" exists and "//" doesn't.

  14. #14
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay i tried copying the address from the addressbar and creating new files, changing the file name but its still the same error. i.e. 2.

    Do i have to set the attributes for the file as well i am trying to read. When i right click the file none of the attributes of read and hidden are selected. Should i select the read option. I mean but this is really weird of being unable to open the file.

  15. #15
    Registered User
    Join Date
    Jul 2009
    Posts
    51
    Don't you have to escape the spaces in file paths in Windows? IE:

    Code:
    file = fopen("C:/Documents\ and\ Settings/ROHAN/Desktop/numbers.txt","r");
    Been a while since I actually hard coded a file path, though, so I may be wrong.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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