fopen question

This is a discussion on fopen question within the C Programming forums, part of the General Programming Boards category; I want to open a file in C:\text.txt and print it. But, it doesn't work. What's wrong in the below ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    fopen question

    I want to open a file in C:\text.txt and print it.

    But, it doesn't work. What's wrong in the below statement?
    thx!


    #include <stdio.h>
    void main()
    {
    int a,b;

    b=fopen("c:\test.txt");
    fscanf(b,"%d\n",&a);
    printf("%d",a);

    getchar();

    }

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    You have the syntax of fopen all wrong. fopen returns a FILE * to the file you opened. The first parameter of fopen is the file, and second parameter (2 parameters in all) is the order of what you want to do ("r" -- read, "w" -- write). The syntax is:

    <FILE * type> = fopen(filename, "r" or "w");

    If you are working with binary files, you just add "b" with the "r" or "w". Your code, revised, would be:
    Code:
    #include <stdio.h> 
    void main() 
    { 
    int a; 
    FILE *fp;
    
    fp=fopen("c:\test.txt", "r"); 
    fscanf(fp,"%d",&a); 
    printf("%d",a); 
    
    getchar(); 
    
    }
    Understand???

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    thanks for reply !

    i am using Visual Studio 6 and win 98 to run the program .

    but it says Expression: str != NULL

    can't find FSCANF.C.
    However, I can't find FSCANF.C in my computer.

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Post all of your code.
    1978 Silver Anniversary Corvette

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    fscanf is a function in the stdio.h header library.
    1978 Silver Anniversary Corvette

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    101

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Ummm...bad link. Just post the code. What is the bmp?
    1978 Silver Anniversary Corvette

  8. #8
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    also... watch out for the backslash... the compiler will think that's an escape sequence, so you need to use a double backslash to get it...
    hasafraggin shizigishin oppashigger...

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Originally posted by Garfield
    Ummm...bad link. Just post the code. What is the bmp?
    Just the code which u told me.

    Code:
    #include <stdio.h> 
    void main() 
    { 
    int a; 
    FILE *fp;
    
    fp=fopen("c:\test.txt", "r"); 
    fscanf(fp,"%d",&a); 
    printf("%d",a); 
    
    getchar(); 
    
    }

  10. #10
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    As DA said, you have to use \\
    Code:
    fp=fopen("c:\\test.txt", "r");

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    I should use this method??

    #include <stdio.h>
    void main()
    {
    char a[200];
    FILE *fp;

    fp=fopen("c:\\test.txt", "r");
    fscanf(fp,"%s",&a);
    printf("%s",a);

    getchar();

    }


    but, how can I read all the texts in a txt file since there is a limit for array?

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    And I just can read line 1 only.

  13. #13
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Fixed code:

    #include <stdio.h>
    void main()
    {
    char a[100];
    FILE *fp;

    fp=fopen("c:\\test.txt", "r");

    while (fscanf(fp,"%c",&a) != EOF)
    {
    printf("%c",a);
    }
    getchar();
    fclose(fp);
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Originally posted by GaPe
    Fixed code:

    #include <stdio.h>
    void main()
    {
    char a[100];
    FILE *fp;

    fp=fopen("c:\\test.txt", "r");

    while (fscanf(fp,"%c",&a) != EOF)
    {
    printf("%c",a);
    }
    getchar();
    fclose(fp);
    }
    thx, but it appears some strange characters.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    I did something like this:

    Code:
    #include <stdio.h> 
    
    void main() 
    { 
    char a[500]; 
    FILE *fp; 
    
    fp=fopen("c:\\test.txt", "r"); 
    
    while (fscanf(fp,"%s",&a) != EOF) 
    { 
    printf("%s",a); 
    } 
    getchar(); 
    fclose(fp); 
    }
    It can read the texts from a .txt file.
    However, it prints all the texts in 1 line.

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

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM

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