C - fprintf isn't writing to file - c

C - fprintf isn't writing to file, any idea why?
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE* pfile=fopen("/home/user-vlad/Programming/C-other/meme.txt","r");
if(pfile==NULL){
printf("ERROR: Stream is equal to NULL\n");
exit(1);
}
fprintf(pfile,"Hello");
fclose(pfile);
return 0;
}
Compiler: clang, OS: FreeBSD

Assuming the file opens can be because you called fopen() with the argument "r", that means read.
To write you can use the argument "w"
fopen("/home/user-vlad/Programming/C-other/meme.txt","w");
Or if the file already exists "r+"
fopen("/home/user-vlad/Programming/C-other/meme.txt","r+");
Or if the file already exists and you want to append you can use "a"
fopen("/home/user-vlad/Programming/C-other/meme.txt","a");
You can learn more on fopen() here.

Related

How to write something in a existing .txt file with c program?

I am trying to write "File opened" in a existing test.txt file with C program. But I must not create a test.txt file with the program. I have to write with in a existing file.
I tried but can't.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000] = "File opened";
fopen("test.txt", "w");
fprintf("%s", sentence);
fclose();
return 0;
}
The error showing me is:
error: too few arguments to function 'fclose'
How can I do that? Please help me.
In future recommend that you do some searching for either existing questions that people have asked or some research. ie look up the manual or spec for fopen: https://man7.org/linux/man-pages/man3/fopen.3.html
Especially since your code doesn't compiled as it has errors, with the fopen, fprintf and fclose all missing arguments or assignment variables. Reading those errors and the compiler messages would have guided you to the solution. This is what it would look like with those fixed and the "w+" option used:
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000] = "File opened";
FILE *file = fopen("test.txt", "w+");
fprintf( file, "%s", sentence);
fclose( file );
return 0;
}
This is a good tutorial if you want to know more: https://www.geeksforgeeks.org/basics-file-handling-c/

Opening a .txt file in Xcode using C and fopen( )

I can't seem to get a file to open in C. What am I doing wrong? The file is in the same directory as the .c file and I think I got all the syntax. Here is a screenshot:
The output says that the file pointer is NULL.
For that to work, the "test.txt" has to be in the same directory as the compiled binary (which xcode may not be putting in the same directory as main.c - it may be in Products? I'm not so sure with xcode). Try giving the fully-qualified pathname to test.txt in the call to fopen.
If fopenfails, fp is set to NULL and errno is set according. To see why, try:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int
main( void )
{
int status = EXIT_SUCCESS;
FILE *fp = fopen(“test.txt”, “r”);
if( fp == NULL )
{
fprintf( stderr, “Errno %d, Error %s, opening text.txt for reading.\n”, errno, strerror(errno));
status = errno;
}
// Do something with fp...
return(status);
}
To open the file from any directory, pass the file name in argv, check for arguments and use that parameter to main as the file name( pref. after copying to a dedicated variable).

C fopen() unable to open file in /tmp

I am following the basic C programming tutorial on tutorialspoint.com
I have the following program which generates a file in /tmp called test.txt:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("/tmp/test.text", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
return 0;
}
Then I have a second program which just tries to open that file for reading:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {
FILE *fp = NULL;
fp = fopen("/tmp/test.txt", "r");
if (fp == NULL) {
printf("NULL!!!\n");
}
printf("%s\n", strerror(errno));
fclose(fp);
return 0;
}
However, when I try to run the program that opens the file, I get the following output:
NULL!!!
No such file or directory
Segmentation fault
If I modify the code to point to the same file in my home directory, it works correctly. It seems that, for some reason, I am not able to open files in the /tmp directory (via fopen)... And just to be clear, I am able to change to /tmp and cat the contents of the test.txt file just fine. Permissions look normal on it as well, 664 with my user as the owner and group.
The only other specifics that I can think of that might have to do with my system is that I am on Elementary OS Juno, I am using g++ 7.3.0 (clang also gives the same result), and I have separate encrypted partitions for my OS root and home...
Any thoughts on what might be causing this?
This was simply caused by an incorrect file extension, .txt vs.text as pointed out by #yano

How to transfer parameters from execl to scanf?

I want to transfer some parameters from one program to another.
For example, here are two programs. a.c compiled as a
#include <stdio.h>
int main() {
char a[10];
scanf("%s", a);
printf("%s\n", a);
return 0;
}
and e.c compiled as e:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main() {
char *input = "here it is";
execl("./a", "a", "a", NULL);
return 0;
}
Actually, I would like to transfer the parameter "a" from the e.c to a.c so that once I execute the program "e", it will print out
a
a
However, I find out that excel cannot pass the parameters to the specified program.
If I cannot modify the program a.c, how can I execute this program using another program using standard input?
If you can't modify a.c, then you'll need to write the data to some file, then reset stdin to read from that file:
FILE *fp = fopen("somefile", "w");
if (fp == NULL)
// handle error
fputs(input, fp);
fclose(fp);
if (freopen("somefile", "r", stdin) == NULL)
// handle error
unlink("somefile");
execl("./a", "a", NULL);
The unlink system call makes sure the file disappears when a is done with it.
Note: you might be able to use a pipe instead of a file, because the message is so small that it will fit inside a pipe's buffer. That's not a reliable solution for larger messages, though -- if you want to use a pipe, you need to fork.

Why is my file pointer causing an undefined symbol error?

This is my code so far:
#include <stdio.h>
int main(void)
{
char filename[50]; /* for holding file's name */
FILE *fp; /* fp is the "file pointer" */
printf("Please enter the name of an input file: ");
scanf("%s", filename);
if (!(fp = fopen(filename, "w"))) /*w=write*/
fprintf(stderr, "unable to open file\a\n");
else {/* process file */
fprintf(fp, "Testing...\n");
}
return 0;
}
The line
FILE *fp;
//is giving me an error Undefined Symbol "FILE"
The line
fprintf(stderr, "unable to open file\a\n");
//is giving me an error Undefined Symbol "stderr"
I thought these keywords were standard C/C++? Why are they giving me errors?
Did you #include <stdio.h>? Also your declaration of main() is incorrect. It should return int, not void.
And no, FILE is not a keyword in either C or C++. Its declaration is in <stdio.h>.
Please add the following line as your 1st statement in your file
#include <stdio.h>
The datatype FILE and functions such as fprint() are defined in this header file and hence you would need that to run your program (tell the compiler the definition of FILE, fprintf() etc)
I had this problem in Visual Studio Code (version 1.67.1) because my c_cpp_properties.json had the "compilerPath" set to "cl.exe", but my tasks.json had "command" set to "clang.exe".
I resolved my issue by copying the path to clang.exe from "command" in tasks.json, and using that for the "compilerPath" value in c_cpp_properties.json.

Resources