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!
Related
I don't understand why is marking as identifier "FILE" is undefined. Firstly I thought it was because of the includes, but in my code I include <stdio.h>. Then I thought that it was just a "marking" squiggle, but when I execute in the terminal shows segmentation fault, so I don't know what I can do.
Here is my program:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
FILE *fp;
fp = fopen("taula.txt", "W");
for (double i = -0.001; i < 0.001; i += 0.00001) {
fprintf(fp, "%lf %.14lf \n", i, -pow(i,4)*1/(840)+(i*i)*1/(30)-1/3);
}
fclose(fp);
return 0;
}
I'm using Visual Studio Code 1.71.2 (Universal), clang as compiler and the OS I use is macOS Monterey 12.6 in MacBook Pro with M1 Pro.
I hope someone can solve my problem.
The error reported for FILE seems unwarranted. Check these possibilities:
maybe the compiler cannot find the standard header files, but it should report this as an error too.
is there is an empty file called stdio.h somewhere in your include path?
Note also these problems:
to open the file for writing, you should use "w", not "W".
you should test for fopen failure, which probably happens because of the above mistake.
the number of iterations in the loop may not be exactly 200 because of cumulative errors adding 0.00001, which cannot be represented exactly using binary floating point representation.
the expression -pow(i,4)*1/(840)+(i*i)*1/(30)-1/3 seems incorrect: 1/3 evaluates to 0 because it uses integer arithmetics.
Here is a modified version:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *fp = fopen("taula.txt", "w");
if (fp == NULL) {
fprintf(stderr, "cannot open taula.txt: %s\n", strerror(errno));
return 1;
}
for (double j = -100; j < 100; j += 1) {
double i = j / 100000.;
double i2 = i * i;
fprintf(fp, "%f %.14f\n", i, -i2*i2/840. + i2/30. - 1./3.);
}
fclose(fp);
return 0;
}
I cannot say why it claims that FILE is undefined — on that front, your code looks fine to me, and it compiles without issues (and since you can run it, I assume it must have compiled for you too, which it wouldn't have if FILE was indeed undefined).
It does, however, segfault. I strongly recommend that you check that the return value of fopen isn’t NULL, and, when you have found out that it is, that you read the fopen manpage carefully, especially the section on what the legal values for the second argument are.
EDIT: And the comment about constants being ints is worth listening to, even if that’s unlikely to cause segfaults.
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/
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
I wrote read this code from the K&R book. But i compile it i get an error:
gcc: error: getchar.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.
Code:
#include <sys/syscall.h>
#include <stdio.h>
int getchar(void)
{
char c;
return (read(0, &c,1) == 1) ? (unsigned char) c : EOF ;
}
main()
{
printf("\nEnter the character you want to getchar: \n");
getchar();
return 0;
}
The error message is telling you that there is no file called getchar.c in the current directory to compile. That might mean that you accidentally called it getchar.c or getchar.c instead, so you should look carefully at what you called it -- it might look like it is correct, but have extra invisible characters in it.
The easiest fix is probably to open the file in your editor, and then "save as" and type in a name that has no invisible characters in it.
If you're just compiling it in the Windows environment
try
#include <sys/syscall.h>
change to
#include <io.h>
I am having an issue passing command line arguments to my program using Visual C++ Express 2010. I found the command arguments under debugging and using the following input, just the terms with white space between them. The file is in my project folder with the .c source code.
TestFile1.txt 2
The program works fine when I just statically define the char pointer under main. So at this point I'm not sure if the issue is with 2010 or the code. I haven't figured out a way to compile and execute in some other way to test command line args. It would be great if someone could compile this an see if it works on their system.
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 256
int main(char *argv[])
{
//char *argv[] = { "program", "TestFile1.txt", "2" };
char buf[BUFFER_SIZE];
FILE *inFp;
printf("%s",argv[1]);
if ((inFp = fopen (argv[1], "r")) == NULL)
{
fprintf(stderr, "Can't open file\n");
exit(EXIT_FAILURE);
}
fclose(inFp);
return 0;
}
it should be int main(int argc, char *argv[]) Other than that, I have not seen any other problem with your program.