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

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/

Related

Debug Assertion Failed: file reading in C using CLion

This is the code that was provided as starter code on which I have to answer a couple of question using the data provided in data.txt file. data.txt is kept in the same folder as my code and contains line separated words all of which are of length 21. I am familiar with C but I do not know anything related to files and file management.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main(void) {
char* fname = "data.txt";
FILE *fptr = NULL;
char line[20000][22];
int i = 0;
fptr = fopen(fname, "r");
while(fgets(line[i],20000,fptr))
{
line[i][strlen(line[i]) - 1] = '\0';
i++;
}
printf("Read a file with %d lines.\n",i);
}
When I run this code in CLion, the following error appears.
I am using LLVM clang compiler. The same error appears even if I use Microsoft Visual Studio compiler. It would be helpful if you can explain what the error means and how to resolve it!

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

C - fprintf isn't writing to file

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.

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