Thread: question about open file in unix system

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    16

    question about open file in unix system

    Hi,all
    I use fopen to open a txt file in Unix , but it always fails (ipf =@NULL ). The file 1.txt is already in the current directory.
    I don' t know why. Can you give some advice?

    Thanks,

    --------------------------------------------------
    char *File1=NULL,*File2=NULL;
    FILE *ifp=NULL,*ofp=NULL;

    File1= "./1.txt";
    File2= "./1-1.txt";

    ifp=fopen(File1,"r");
    ofp=fopen(File2,"w");

  2. #2
    Unreg1
    Guest
    char File1[] = "./1.txt";

    and the same for the other file.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Try two '/', I'm on windows at present and can't verifiy.

    File1= ".//1.txt";
    File2= ".//1-1.txt";
    Last edited by Scarlet7; 03-12-2003 at 02:57 AM.

  4. #4
    Stewdent
    Guest
    this works for me on UNIX

    Code:
    #include <stdio.h>
    
    int main()
    {
        FILE *fp1, *fp2;
        char *filename1 = "./1.txt";
        char *filename2 = "./1-1.txt";
        
        if( (fp1 = fopen( filename1, "r" ) ) == NULL)
            printf("Unable to open file.\n");
        else printf("File %s is open for reading.\n", filename1);
        
        if( (fp2 = fopen( filename2, "w" ) ) == NULL)
            printf("Unable to open file.\n");
        else printf("File %s is open for writing.\n", filename2);
                 
    
        return 0;
    }
    hope this helps

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    printf("Unable to open file.\n");
    To print out error, try using the function perror(), is better.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    16
    Originally posted by Vber
    To print out error, try using the function perror(), is better.
    Thanks.

    I have used it, can you tell me how to know the decsription of the error?

    errno =@0x290010

    It happens when fclose ends.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post some of your code. Also, this is how it is supposed to look:
    Code:
    char *fn = "myfile.txt";
    
    if ((fp = fopen(fn, "r")) == NULL)
    {
      perror (fn);
      return (EXIT_FAILURE);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM