Thread: yes, its true! i cant read files! *cry*

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    yes, its true! i cant read files! *cry*

    ok, i have no idea how to read from a file, and ive never gotten it to work. here is my code, maybe you guys can make something of it. (its probably a very simple thing, knowing me.)


    #include <stdio.h>
    #include <ctype.h>

    int main(){

    int c;
    FILE *fp;
    char filename[81];

    gets(filename);


    fp = fopen(filename, "r");

    while ((c = fgetc(fp)) != EOF)
    putchar(toupper(c));

    fclose(fp);
    return 0;

    }


    most of this i got from a book. i edited the file to the bare minimum. i put in a string, and it opens the file of that string, but it never can open the file. that code gives an illegal operation, but i used to have it to where it would give error messages if it couldnt find the file, and it would give it every time.
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    fp = fopen(filename, "r");
    
    if(fp == NULL){
    	printf("Could not open file");
    	return 0;
    }
    That addition will tell you if the program was able to find the file

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >but i used to have it to where it would give error messages if it couldnt find the file, and it would give it every time

    You must be incorrectly typing the file path. The code works for me.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    i am typing in "c:\folder\textfile.txt" (without the quotes)

    what am i supposed to type?
    I came up with a cool phrase to put down here, but i forgot it...

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    If that's the correct name and location of an existing file then it should work.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    and you say it works for you, eh?....


    are you talking about my program specifically works for you, or just the fopen, etc.., in general?
    I came up with a cool phrase to put down here, but i forgot it...

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    No, I'm talking about your program.

  8. #8
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    maybe double slashes?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I don't see anything wrong aside from the fact that you don't test the return value of fopen and you used gets *gag*.

    On my system this code will accept and run all three slash variations.
    c:\Program Files\Microsoft Visual Studio\MyProjects\cTest\test.txt
    c:\\Program Files\\Microsoft Visual Studio\\MyProjects\\cTest\\test.txt
    c:/Program Files/Microsoft Visual Studio/MyProjects/cTest/test.txt

    Are you sure you're typing a valid file path?

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read files
    By malebo in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2008, 12:25 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. How to read files?
    By Raigne in forum C++ Programming
    Replies: 17
    Last Post: 11-23-2005, 05:03 PM
  5. Replies: 3
    Last Post: 05-05-2004, 05:40 PM