Simple C Question

This is a discussion on Simple C Question within the C Programming forums, part of the General Programming Boards category; How can i open a file for reading and writing in a directory other than "C:\"?? This is the only ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Simple C Question

    How can i open a file for reading and writing in a directory other than "C:\"??

    This is the only way I know how to do this...


    Code:
    if((fp = fopen("C:\hello.txt, "w")) != NULL)
    {
         
         fprintf(fp, "Hello.");	
         fclose(fp);
    
    }
    
    else
         puts("Write Failed.");
    So when I do that all i can read and write are files in C:\ and no other directory...like C:\whatever...if i try that it fails. To clarify, If i were to write C:\directory\text.txt in up there, it fails. I also tried C:\\directory\\text.txt but that fails too.

    Thanks.
    Last edited by almo89; 02-10-2006 at 11:44 PM.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    [code]
    Code:
    if((fp = fopen("C:\hello.txt, "w")) != NULL)
    You only have the 1 " which would be causing problems. Use 2 and put whatever directory you want to write/read a file in, inside them like.

    [code]
    Code:
    if((fp = fopen("D:\blah\hi.txt", "w")) != NULL)

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    "c:\\directory\\test.txt" should work, as should "c:/directory/test.txt". If it fails, look at the error reason. You can use the perror() function to do this.

    Edit:

    Quote Originally Posted by John_
    Code:
    if((fp = fopen("C:\hello.txt, "w")) != NULL)
    You only have the 1 " which would be causing problems. Use 2 and put whatever directory you want to write/read a file in, inside them like.

    Code:
    if((fp = fopen("D:\blah\hi.txt", "w")) != NULL)
    You must use two backslashes otherwise they'll be treated as special characters: "D:\\blah\\hi.txt". For that reason, "d:/blah/hi.txt" is a lot more readable and generally preferred.
    Last edited by cwr; 02-10-2006 at 11:49 PM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Oops

    Haha yah sorry...thats not actually in my program that was a typo..i have that and it still doesnt work...this is exactly what is in my program...

    Code:
    int main (void)
    {
    	FILE *fp;
    
    		
    	if((fp = fopen("C:\folder\text.txt", "w")) != NULL)
    	{
    
    	fprintf(fp, "Hello.");	
    	fclose(fp);
    
    	}
    
    	else
    		puts("fail");
    
    	return 0;
    }
    So when that is run...i get the FAIL message.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by almo89
    Code:
    if((fp = fopen("C:\folder\text.txt", "w")) != NULL)
    A point to consider: what are \f and \t? [Form feed and tab?]

    Use forward slashes.
    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.*

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Oh yea I forgot the extra \ for an esacape character in my example. And I also messed up the code tags hehe. I'll leave my original reply as is for consistency, you guys have given the correct answer anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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