Re: Newbie problem, fopen
Quote:
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.
Quote:
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.
Quote:
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.)
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
Re: Newbie problem, fopen
Well yes, compiling the program isn't the same thing as running it.
Re: Newbie problem, fopen
Haha yea ...every day is a school day!
Seán
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;
}
Re: Newbie problem, fopen
Seems like you want fprintf rather than fputs.
Re: Newbie problem, fopen
Still didn't like that. Here is the error
Quote:
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
Re: Newbie problem, fopen
Read the printf documentation more carefully. You aren't passing the arguments in the correct order.
Re: Newbie problem, fopen
Ahh, got it!
Go raibh mÃ*le maith agat mo chara!
(A thousand thank yous my friend!)
Seán
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
Re: Newbie problem, fopen
You have a file called "open" in the working folder?
Re: Newbie problem, fopen
Hello,
Yes, I do ...I tried calling it 'open' and open.txt', but nothing doing.
Seán
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
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
Re: Newbie problem, fopen
To be absolutely sure, print out the current working directory within the program.
On Linux, it's getcwd() in unistd.h:
http://linux.die.net/man/3/getcwd
On Windows, use _getcwd() in direct.h:
http://msdn.microsoft.com/en-us/library/sf98bd4y.aspx
Sometimes, Windows will hide file extensions so that the name of the file isn't what it appears to be, eg, "hello.txt" may show up in Windows as "hello" but with a text-file icon. In this case if you had called the file "hello.txt", its real name may have been "hello.txt.txt". Make sure this isn't the case; fopen will recognize only exactly identical file names.
Re: Newbie problem, fopen
Well, you were right about the file extensions ...I went through the Windows control panel and displayed all file extensions, so my file was 'open.txt.txt' ...stupid Windows!!
Anyway, when I run the .exe file, the consol window pops up saying 'Didn't open'.
Code:
/* fopen example, exercise 4 */
#include <stdio.h>
int n;
int main ()
{
FILE *pFile;
pFile = fopen ("open.txt.txt","r");
if (pFile!=NULL)
{
printf("Didn't open",pFile); //Display if don't open
fclose (pFile);
scanf("%d%",n); //Used to keep consol open
}
return 0;
}
Seán