How Can I Open File with fopen() and add something to my file and See That????????
12 21210 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.
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?
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?
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.
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 - int main(void) {
-
-
char buffer[100];
-
FILE *fp;
-
-
fp = fopen("functions.txt", "a+");
-
-
if ( fp != NULL ) {
-
cout << fgets(buffer, 100 , fp);
-
}
-
-
}
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
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?
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.
Where i can find the file i opened with my program??
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+");
Sign in to post your reply or Sign up for a free account.
Similar topics
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
...
|
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')...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |