-
October 26th, 2011, 12:09 PM
#1
Newbie problem, fopen
Hello,
I am hoping someone can help with a problem I am having. I need to open a file to write an int and a float to it. So, I have been trying to build up my understanding to do this, so I figured I would try and get a small prohram working first. But that plan isn't going so well.
The program is
Code:
/* fopen example */
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
{
fputs ("fopen example",pFile);
fclose (pFile);
}
return 0;
}
Now, can I ask, do I need to create an empty file called myfile? Or shoukd the program do that? If I do need to make the file, how does the program know where to find it??
I have been reading the documentation, but I don't really find answers to these questions, they all assume a specific amount of understand to begin with it seems.
Any help or advice would be fantastic! I want to learn how to do this, I don't want anyone to do it for me, but I do need to have some things explained.
Also, is there an error in this program??
I hope that's cool.
Thanks in advance.
Seán
-
October 26th, 2011, 12:33 PM
#2
Re: Newbie problem, fopen
Originally Posted by o.fithcheallaigh
Now, can I ask, do I need to create an empty file called myfile? Or shoukd the program do that?
Since you opened the file for writing (with "w"), a file will be created if it doesn't already exist. The "w" flag will also cause any existing file to be completely overwritten. If you had opened it with "a" instead (append), a file would be created if necessary but if it already existed, your additional output would just be added, rather than replacing what was there before.
If you had opened the file for reading with "r", the file would not be created if it did not already exist. The fopen would simply fail.
If I do need to make the file, how does the program know where to find it??
It uses the current working directory. If you are running your program from the command line, that is whatever location you are in before launching the program. With an IDE, it is usually the project directory or something close to it. You can change the CWD if you want via project options or API calls.
Also, is there an error in this program??
Looks okay at first glance. Does it work? (I don't recall if FILE* stuff requires stdlib.h or not. It might.)
-
October 26th, 2011, 01:59 PM
#3
Re: Newbie problem, fopen
Hey,
Thanks very much for the reply, and the information.
Actually, it did work. However, I didn't know it did ...I complied the program a few times, but no file was created in the folder ....then, I clicked on a .exe file that was the project and that created the file.
Again, thanks a lot for your help!
Seán
-
October 26th, 2011, 02:08 PM
#4
Re: Newbie problem, fopen
Well yes, compiling the program isn't the same thing as running it.
-
October 26th, 2011, 02:10 PM
#5
Re: Newbie problem, fopen
Haha yea ...every day is a school day!
Seán
-
October 26th, 2011, 02:45 PM
#6
Re: Newbie problem, fopen
If I can ask another question...
I have been putting the full program together, to read in an int and a float, and then write them to a file. But this line isn't liked.
Code:
fputs("Your integer was: %06d and your float was: %5.2f",x,y,pFile);
The error says there are two many arguments. Is there an alternative that will allow me to do what I need.
The full peogram is below
Code:
#include <stdio.h>
int x; //Variables declared
float y;
int main ()
{
printf("Please enter and integer and a floating point number\n");
scanf("%d", &x); //Scanning for input, saving to x
scanf("%f", &y); //saving to y
//Below will display the information in required format
/*printf("Your integer was: %06d and your float was: %5.2f", x, y);
scanf("%d", x); //Used to keep the consol window open*/
FILE * pFile;
pFile = fopen ("myfile.txt","w");
if (pFile!=NULL)
{
fputs("Your integer was: %06d and your float was: %5.2f",x,y,pFile);
fclose (pFile);
}
return 0;
}
-
October 26th, 2011, 02:45 PM
#7
Re: Newbie problem, fopen
Seems like you want fprintf rather than fputs.
-
October 26th, 2011, 02:55 PM
#8
Re: Newbie problem, fopen
Still didn't like that. Here is the error
23 C:\Users\Seán\Documents\Cpp\1\main.cpp cannot convert `const char*' to `FILE*' for argument `1' to `int fprintf(FILE*, const char*, ...)'
Now, I am not sure what that means.
Seán
-
October 26th, 2011, 02:56 PM
#9
Re: Newbie problem, fopen
Read the printf documentation more carefully. You aren't passing the arguments in the correct order.
-
October 26th, 2011, 03:11 PM
#10
Re: Newbie problem, fopen
Ahh, got it!
Go raibh mÃ*le maith agat mo chara!
(A thousand thank yous my friend!)
Seán
-
October 28th, 2011, 11:50 AM
#11
Re: Newbie problem, fopen
Hello,
Thought I would bring this one back since it is a related question.
I am now trying to open a file which I have created in the working folder. But it won't open. Can someone see what I am doing wrong?
Code:
/* fopen example, exercise 4 */
#include <stdio.h>
int n;
int main ()
{
FILE *pFile;
pFile = fopen ("open","r");
if (pFile!=NULL)
{
printf("Didn't open",pFile); //Display if don't open
fclose (pFile);
scanf("%d%",n); //Used to keep concol open
}
return 0;
}
Thanks in advance.
Seán
-
October 28th, 2011, 12:12 PM
#12
Re: Newbie problem, fopen
You have a file called "open" in the working folder?
-
October 28th, 2011, 12:42 PM
#13
Re: Newbie problem, fopen
Hello,
Yes, I do ...I tried calling it 'open' and open.txt', but nothing doing.
Seán
-
October 28th, 2011, 12:50 PM
#14
Re: Newbie problem, fopen
Are you using an IDE (eclipse, MSVS, etc...)? I have a feeling that the cwd (current working directory) for debugging is not pointing at the same directory as where your file "open" is.
To verify, at a command console, in the same directory as the executable, have your "open" file, then execute the programme. Does this work?
A
-
October 28th, 2011, 01:07 PM
#15
Re: Newbie problem, fopen
Hello,
Yes, I am using an IDE ...from Bloodshed.
I'm sorry, I am not sure what you want me to do ...but I have everything in the one folder, my executable file, the file I want to open etc.
And it worked fine when I was creating a folder.
Seán
Last edited by o.fithcheallaigh; October 28th, 2011 at 02:18 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|