problem with fopen and absolute path

This is a discussion on problem with fopen and absolute path within the C Programming forums, part of the General Programming Boards category; Hi every one i have a problem with relatives path to files when i try to open a file in ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    problem with fopen and absolute path

    Hi every one
    i have a problem with relatives path to files when i try to open a file in c.
    Below will follow the code taken from a bigger program
    Code:
    FILE *infil;
    char filname[100] = "C:\\temp\\testdata\\";
    strcat(filname, readme.txt);
    printf("%s \n", filname);
    infil = fopen(filname, "rb"); // tested also with only r
    if (infil== NULL)
    	{
    	printf("Error: can't open infile.\n");
    
    	}
    Why is infil null. The path is correcte and i can see also when when i copy and paste from printf("%s \n", filname) . By the way you need to have \\ in the path above because C\... with only one \ doesn't work so the error is not there. Some research with google didn't approach me to the solution so i hope therefore that some of you can answer on this question
    Last edited by kobra_swe; 04-15-2008 at 02:31 PM.

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    C:\\temp\testdata\

    Also, I'm not sure, since I've not worked with strcat, but I would assume you would need " " around readme.txt.

    Also:
    Code:
    infil = fopen(filnamn, "rb");
    Last edited by JDGATX; 04-15-2008 at 01:18 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Not only the filnamn as mentioned, but a filename can't end with a backslash -- things that end with backslashes are directories. EDIT: Okay, there's a strcat to add the filename itself. So that's fine, except you need "readme.txt" in quotes.
    Last edited by tabstop; 04-15-2008 at 01:23 PM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Maybe replace
    Code:
    printf("Error: can't open infile.\n");
    with
    Code:
    perror(filname);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    You may wish to stick to either swedish or english variable names
    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.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Can we make a rule around here that we aren't going to comment on code that isn't even real? That code is uncompilable, inconsistent even within itself (multiple misspellings of "filename"). It is a complete waste of time to even speculate on what has gone wrong, since you haven't given us anything except some random meaningless typing.

  7. #7
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by Elysia View Post
    You may wish to stick to either swedish or english variable names
    And I just thought he hated the letter 'e'.

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    the code is

    consistens but when i cut from my editor i changed some swedish terms to english and i forgot to do it in all places. But in the original version in every place it is the same variable (filnamn). so this misspelling was a miss in my code here not in the one i have compiled before. A can excuse all my misspelling with the fact that i am very tired right now
    In my code readmy is contained i a char* variable so i give the variable as argument and the printf("%s \n", filname) gives correct output in the screen. when icopy and paste the output of the print statements in file browser i can open the file readme.txt without any problem. So the error doesn't seems to be there. I guess that the path needs to be given in some special format but i can not see how this format should be even with the help of google
    Last edited by kobra_swe; 04-15-2008 at 02:43 PM.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    For future reference, the language of the variables is not particularly important to us in helping you; the actual code in use is of great importance.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    I don't care about misspellings. But seeing it spelled multiple ways proves that this can't possibly be the real code.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this also a typo, as it would not compile like this [unless you have a struct with a member txt, of which there is an instance called readme - but I find that rater unlikely]:
    Code:
    strcat(filname, readme.txt);
    As I would expect it to be:
    Code:
    strcat(filname, "readme.txt");
    Assuming the directory c:\temp\testdata and contains a file "readme.txt", then it should work - there's nothing directly wrong (typos aside) in your code - of course, a small typo can easily cause the code to not work correctly, so check carefully, and if you you still can't make it work, post the code you are actually using here - swedish or english or greek variable names doesn't matter [although there are at least three people here that will understand Swedish variable names, me included, we're quite used to people posting variable names that are one or two letters, which is often worse than descriptive names in an unknown language - at least it's easier to recognise a long name than a short one when you see it further down].

    --
    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.

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    this problem is solved

    the settings in file browser was not to show file extension.This means that when i created text files i called them file.txt, The result of this was of course that the name of the file became fil.txt.txt not matching the argument. Maybe this can be a hint to other people

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Sigh, Microsoft's default stupid settings. Even though they are a security risk.
    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.

  14. #14
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Another thing to check, as I did not see this mentioned, do you have the proper access to read this binary file?

    What user is this readme.txt file associated with and what association is your compiled .c program with?

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Sigh, Microsoft's default stupid settings. Even though they are a security risk.
    Are you saying that showing or hiding filename extensions is a security risk? I can't quite tell . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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

Similar Threads

  1. File Path Setting
    By blue_gene in forum C Programming
    Replies: 11
    Last Post: 12-23-2003, 06:04 PM

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