Unable to read from a file

This is a discussion on Unable to read from a file within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to open a file using C. Code: FILE *fp; fp = fopen("file://localhost/Users/Desktop/CPrograms/names.txt","r+"); if(fp) { printf("Yes"); } ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    Unable to read from a file

    Hi,

    I am trying to open a file using C.

    Code:
    FILE *fp;
    	
    	fp = fopen("file://localhost/Users/Desktop/CPrograms/names.txt","r+");
    	
    	if(fp)
    	{
    		printf("Yes");
    	
    	}
    	else{
    		perror(NULL);
    		printf("NO");
    	}
    whenever i try compiling and checking what happens all I am getting is "No such file or directory" even though the file exists and i have permissions on it.

    Does anyone have any idea why this is happening or how to avoid such an error?

    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Why are you using browser syntax "file://" at the start of the pathname?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Get rid of the "file://", Use the right slash and duplicate the slashes: localhost\\Users\\Desktop\\CPrograms\\names.txt"," r+");

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    You should review how to open a file.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A string literal, is just that - a literal. So unless your file name is:
    file://localhost/Users/Desktop/CPrograms/names.txt

    then that file will never be located.

    You can use either the single / slash (Windows or Linux), or the doubled backslash \\, in your string literal.
    Code:
    fp = fopen("localhost/Users/Desktop/CPrograms/names.txt","r+");
    If localhost is on another drive or directory, then add that to the start of the string literal.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    Thanks guys for the help.

    Adak I tried using your way
    [CODE]
    fp = fopen("localhost/Users/Desktop/CPrograms/names.txt","r+");
    [\CODE]
    but it also didn't work

    and Libpgeak i tried using your way as well and still didn't work

    but i just used
    [CODE]
    fp = fopen("names.txt","r+");
    [\CODE]

    and now it's working eventhough i tried it before and didn't work for some reason or another.

    Thanks a lot guys

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So now you know that "localhost" is not the directory your program is being run in, don't you?

    And you won't be using these kinds of things in string literal names for files, anymore, will you?

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I suppose the localhost - is just localhost the equivalent for 127.0.0.1 and user tries to open file using its UNC path

    \\127.0.0.1\Users\Desktop\CPrograms\names.txt

    since this path leads to some location on the local computer I do not really see a reason not to use the actual path, something like

    C:\Users\Desktop\CPrograms\names.txt

    I do not really sure that all implementations of fopen know how to access files through UNC
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to open included file
    By jaisha in forum C Programming
    Replies: 12
    Last Post: 04-22-2010, 12:23 AM
  2. unable to write to a file
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 08:18 AM
  3. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  4. Unable to open a file for reading
    By raghuveerd in forum C Programming
    Replies: 16
    Last Post: 06-18-2007, 11:47 PM
  5. unable to read double A[0] and A[1] when n=1
    By sweetarg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 12:35 PM

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