7 Replies - 297 Views - Last Post: 12 May 2013 - 09:52 AM Rate Topic: -----

#1 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 11-May 13

File I/O Problem

Posted 11 May 2013 - 09:22 AM

So, I started learning C about a month ago, and am really enjoying it. I ran into an issue I can't find the solution to. When I try to open a file in the code It comes up as a blank program. I have tried using ".txt" ".c" I just can't seem to find the issue. Any help would be appreciated.

#include <stdio.h>
int main()
{
    FILE *fp;
    fp=fopen("C:\\Users\Conner_2\Desktop\Test.txt", "r");
    getch();
    return 0;
}


Thanks, again.

Is This A Good Question/Topic? 0
  • +

Replies To: File I/O Problem

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: File I/O Problem

Posted 11 May 2013 - 09:33 AM

What do you expect your program do? You complain about the program being blank, so I suppose you expected some sort of output instead - is that correct? If so, what output did you expect? Did you expect the contents of the file to be displayed? That's not what fopen does.

fopen simply opens the file and gives you a file pointer that you can then use to work with the file. You can use that file pointer with functions like fgets, fscanf or fgetc to read from the file and you can then use standard output functions like puts or printf to output the characters you read, but fopen does none of those things on its own.

Also note that you should close your file with fclose when you're done using it.
Was This Post Helpful? 1
  • +
  • -

#3 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 11-May 13

Re: File I/O Problem

Posted 11 May 2013 - 09:37 AM

I misunderstood the function of fopen, I appreciate the help.
Was This Post Helpful? 0
  • +
  • -

#4 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 11-May 13

Re: File I/O Problem

Posted 11 May 2013 - 02:14 PM

I've run into another problem. Whenever I add the variable for the fclose function it says that there is a error associated with the variable.
#include <stdio.h>
int main()
{
    FILE *fp;
    int fclose(FILE *Test.txt);
    fp=fopen("C:\\Users\Conner_2\Desktop\Test.txt", "rw");
    fprint(fp, "Hello, Test... Test\n");
    fclose(fp);
    getch();
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: File I/O Problem

Posted 11 May 2013 - 02:22 PM

int fclose(FILE *Test.txt);

I am not sure what your intention is with this line, but it neither declares an integer nor closes a file. Put it this way, there isn't an open file that you can close.
Was This Post Helpful? 1
  • +
  • -

#6 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 11-May 13

Re: File I/O Problem

Posted 11 May 2013 - 05:17 PM

I am clearly confused if someone can give me an example program using, fopen, fclose, fprintf, fread, fwrite, fscanf, fgetc, fputc. It would be greatly appreciated. As the tutorial I'm using is lacking a Example Program.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3177
  • View blog
  • Posts: 9,633
  • Joined: 05-May 12

Re: File I/O Problem

Posted 11 May 2013 - 10:10 PM

Huh? That tutorial actually has several examples in it. You must not have been reading it, but merely scanning it to crib some code from it. If you read it, you would have noticed that it even has a link to a complete program.
Was This Post Helpful? 0
  • +
  • -

#8 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 11-May 13

Re: File I/O Problem

Posted 12 May 2013 - 09:52 AM

I read this tutorial/made this topic on 2 hours of sleep. I now understand File I/O and appreciate all the help anyways!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1