473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Open File with fopen()

57
How Can I Open File with fopen() and add something to my file and See That????????
Jun 25 '06 #1
12 21209
Banfa
9,065 Expert Mod 8TB
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
Kasya
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
Kasya
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
D_C
293 100+
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
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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
Kasya
57
Thanks!!!!
Jun 27 '06 #9
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
nmadct
83 Expert
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
Where i can find the file i opened with my program??
May 10 '07 #12
askcq
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

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

Similar topics

1
by: Cam | last post by:
Hi I'm using this simple rss parser that i would like to have open in another window. But I'm very much a beginner in php scripting. Here is the script on the page <?php ...
1
by: Eric Pederson | last post by:
WryPyY's- I've come across this file operation in others' code and have not seen any documentation for it: the use of "t" in the mode of file opening, such as: f1=open(somefile, 'at')...
2
by: Corne' Cornelius | last post by:
Hi, When you open a file for writing/appending with open() or fopen(), and you have multiple applications that might want to write to the same file at the same time, could that cause weirdness...
10
by: Grocery Clerk | last post by:
I know open() returns a file descriptor and fopen() returns a pointer to FILE. The question is, when do I use fopen() and when do I use open()? Could someone give me an example when to use one...
13
by: Blue | last post by:
Hi , Can any one please let me explain me the diffrences between "open"/ "fopen" or "read"/"fread" or "write/fwrite". I know that "open" /"read" / "write" are system calls and "fopen"...
4
by: Frank | last post by:
Could someone tell me how to open a file at run time that I didn't know the name of at compile time? I know how to open a file at compile time when I know what the name is going to be. FILE...
4
by: Claire DURAND | last post by:
Hi, I try to open a distant (not local) XML file in PHP to read an RSS feed. I can open an HTML page and read it with the file() function. But as soon as I try with a RSS feed instead of...
3
by: siyaverma | last post by:
i am trying to upload csv file from user's computer to main server the code i am using is if(((isset($_GET)) && ($_GET=="yes")) ) { $typefield = $_GET; echo...
7
by: lawrence k | last post by:
I've got a music studio for a client. Their whole studio is run with Macintosh computers. Macintosh computers allow file names to have open white spaces, such as "animal hospital.mp3". I have a...
5
by: chazzy69 | last post by:
The error : http://www.somesite.com Warning: fopen() : php_network_getaddresses: getaddrinfo failed: Name or service not known in /etc/etc/etc/etc/test.php on line 766 Warning:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.