Why is my file pointer causing an undefined symbol error? - c

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.

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 File handling in visual studio 2017

The file handling commands in Visual Studio seem to be different than normal. I'm currently learning the very basics of File Handling in C, but the commands don't seem to be working. This is what I've got right now -
#include <stdio.h>
int main()
{
int num;
FILE *fptr;
fptr = fopen("C:\\", "program.txt", "w");
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf_s("%d", &num);
fprintf(fptr, "%d", num);
fclose(fptr);
return 0;
}
Here's the build output-
'fopen': too many actual parameters
warning C4013: 'exit' undefined; assuming extern returning int
error C4996: 'fopen': This function or variable may be unsafe. Consider using
fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
When I use fopen_s instead, like this fopen_s("C:\program.txt", "w"), it says-
'function': 'FILE **' differs in levels of indirection from 'char [15]'
'fopen_s': different types for formal and actual parameter 1
'fopen_s': too few arguments for call
'=': 'FILE *' differs in levels of indirection from 'errno_t'
I need some serious help.
You should open your file with either
FILE * f;
f= fopen("C:\\program.txt", "w");
or
FILE * f;
int err = fopen_s(&f, "C:\\program.txt", "w");
the latter takes FILE ** as an extra argument, and return error code (0 on success).
There is a extra comma , in fopen() which makes fopen() as three arguments, which is wrong & causing the error
'fopen': too many actual parameters
This
fptr = fopen("C:\\", "program.txt", "w"); /* fopen() expects 2 arguments */
replaces with
fptr = fopen("C:\\program.txt", "w");
You can disable below
'fopen': This function or variable may be unsafe. Consider using
fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
warning by
#pragma warning(disable:4996) /* use it before other headers */
Or use fopen_s().

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.

input redirection on CMD

Well, I am learning programming in C, and I got an assignment to get 3 characters from an input text file into 3 variables and then print their ASCII values.
I wrote this code:
#include <stdio.h>
int main()
{
char a,b,c;
printf("Insert 3 characters:\n");
a=getch();
b=getch();
c=getch();
printf("%d, %d, %d",(int)a,(int)b,(int)c);
}
I opened a text file (input.txt) and wrote there: "abc".
I managed to compile the code with the MinGW compiler, and on the CMD window that I opened in the folder of the .exe file, I wrote: "Task.exe <input.txt".
The program ran normally. I mean, it waited for me to input 3 characters.
What have I done wrong in my work?
help me please :)
You are asked to read from an input text file.
Why don't you use fopen to open a file handle, and fgetc to read from it?
You could perhaps use fscanf. Don't forget to use the resulting count.
And of course, you should call fclose. Using perror is useful to handle error cases.
So start your code with something that checks that your program has an argument, then fopen it:
int main(int argc, char**argv) {
if (argc<2) { fprintf(stderr, "missing program argument\n");
exit(EXIT_FAILURE); };
FILE* fil = fopen(argv[1], "r");
if (!fil) { perror(argv[1]); exit(EXIT_FAILURE); };
Then run Task.exe input.txt in your console (no redirection needed!).
You should take the habit of reading the documentation of every function you are using, of testing failure cases, of compiling with all warnings & debug info (gcc -Wall -Wextra -std=c99 -g), and of using the debugger (gdb).

Resources