By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Open File with fopen()

P: 57
How Can I Open File with fopen() and add something to my file and See That????????
Jun 25 '06 #1
Share this Question
Share on Google+
12 Replies


Banfa
Expert Mod 5K+
P: 8,916
fopen( <FileName>, "a" );

or

fopen( <FileName>, "a+" );

If you want to be able to read as well as write to the file.
Jun 25 '06 #2

P: 57
When i write

int main(void) {

char buffer[100];
FILE *fp;
fopen("functions.txt", "a+");
cout << fgets(buffer, 100 , fp);


}

The Runtime Error Occurs:
Run-Time Check Failure #3 - The variable 'fp' is being used without being defined.

What Should I do?
Jun 26 '06 #3

P: 57
When i write

int main(void) {

char buffer[100];
FILE *fp;
fopen("functions.txt", "a+");
cout << fgets(buffer, 100 , fp);


}

The Runtime Error Occurs:
Run-Time Check Failure #3 - The variable 'fp' is being used without being defined.

What Should I do?
Jun 26 '06 #4

100+
P: 293
D_C
fp is the address of the pointer. *fp is what you defined, I beleive it's also what you want to use. You want to use the contents it points to, instead of it's address.
Jun 26 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
fp is the address of the pointer. *fp is what you defined, I beleive it's also what you want to use. You want to use the contents it points to, instead of it's address.
Nice try D_C but not quite right :-)


Kasya

You declare "FILE *fp" correctly but you never initialise it to anything, so in your fgets call it is just an random value.

The error is on the fopen line, fopen returns FILE * and you need to assign the value to your fp variable. Additionally you need to ensure that the file has actually opened. If it fails fopen returns NULL. Finally using "a+" (as I know I suggested) positions the file pointer at the end of the file ready for appending data to the file. If you want to read the file first before writing to it then you need to either reposition the file pointer or open it using "r+".

I suggest you see the help of the fopen function here.

Anyway putting all that together you getsomething that looks like

Expand|Select|Wrap|Line Numbers
  1. int main(void) {
  2.  
  3.     char buffer[100];
  4.     FILE *fp;
  5.  
  6.     fp = fopen("functions.txt", "a+");
  7.  
  8.     if ( fp != NULL ) {
  9.         cout << fgets(buffer, 100 , fp);
  10.     }
  11.  
  12. }
Jun 26 '06 #7

Banfa
Expert Mod 5K+
P: 8,916
I merged the threads since they are dealing with the same problem.

Next time if you think your problem has been forgotten/overlooked rather than start a new thread please put a new post into the current thread so that it pops up to the top of the list and appears in my list of subscribed threads.

Ta
Ben
Jun 26 '06 #8

P: 57
Thanks!!!!
Jun 27 '06 #9

P: 1
Hi, I am having a similar problem. I am trying to create a new binary file, and insert text into that file using a pointer. This is what I have so far:

char buffer [50];
int *ppointer;
ppointer = fopen ("ILoveThisClass.bin", "w+");
return 0;

The problem is, that I don't know quite where I add the text in. From my understanding, so far I have made my pointer, told it to create the binary file and made it a writable. But then I'm stuck. Any help?
Apr 2 '07 #10

Expert
P: 83
Hi, I am having a similar problem. I am trying to create a new binary file, and insert text into that file using a pointer. This is what I have so far:

char buffer [50];
int *ppointer;
ppointer = fopen ("ILoveThisClass.bin", "w+");
return 0;

The problem is, that I don't know quite where I add the text in. From my understanding, so far I have made my pointer, told it to create the binary file and made it a writable. But then I'm stuck. Any help?
First of all, fopen returns a value of type FILE*, not int*. To make sure that the file is written in binary mode, add a "b" to the mode argument of fopen, so you have "wb+". (You probably won't need the "+" part by the way. Most of the time you open a file for reading OR writing, not both.) You can write to a binary file using the fwrite function. What you will want to do is fill your buffer with data, then use it as the input to fwrite. See the documentation of the fwrite function for more information.
Apr 2 '07 #11

P: 1
Where i can find the file i opened with my program??
May 10 '07 #12

P: 63
int main(void) {

char buffer[100];
FILE *fp;

fp = fopen("functions.txt", "a+");

if ( fp != NULL ) {
cout << fgets(buffer, 100 , fp);
}

}

for the above example you you can place ur file in the current working directiory by giving fp = fopen("\\functions.txt", "a+");
May 10 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.