when I run my Program, which should read a simple File, it just crashes at the fgets() function. I get no errors in my IDE or from gcc. I know that there are similar Posts, but I couldn't figure out the Problem with them. So here is my code, and thanks for help!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char a[64];
int main() {
FILE* fp;
fopen("Memory.mem", "r");
fgets(a, 64, (FILE*)fp);
printf("%s\n", a);
// the getchar is just that the Program doesn't close it self imeadiatly
getchar();
return 0;
}
//In the Memory.mem file is just "abcdefg"
The problem is when you fopen you don't actually save the returned file pointer.
When you call fgets fp is uninitialized, thus causing undefined behaviour.
You can fix it by instead of doing this:
FILE* fp;
fopen("Memory.mem", "r");
do this:
FILE* fp = fopen("Memory.mem", "r");
Also note that it is good practice to put a check if opening the file was successful.
FILE* fp = fopen("Memory.mem", "r");
if(fp == NULL){
printf("Couldn't open file!\n");
return EXIT_FAILURE;
}
And you should close the file once you are done using it with:
fclose(fp);
Related
Help I'm trying to write the data in the file. then, trying to read it back, but its not working.
#include <stdio.h>
int main(void) {
FILE *fptr = fopen("try.txt", "r+");
char line[1000];
fprintf(fptr, "i have new number = 1425");
while (fgets(line, 1000, fptr)) {
printf("%s",line);
}
return 0;
}
You must use a positioning function such as rewind() or fseek() between read and write operations.
Beware that the update mode for streams is very confusing and error prone, you should avoid using it and structure your programs accordingly.
Incidentally, your program will fail to open try.txt if it does not already exist, but you do not check for fopen failure so you will get undefined behavior in this case.
Here is a modified version:
#include <stdio.h>
int main(void) {
char line[1000];
FILE *fptr = fopen("try.txt", "w+");
if (fptr != NULL) {
fprintf(fptr, "I have new number = 1425\n");
rewind(fptr);
while (fgets(line, sizeof line, fptr)) {
printf("%s", line);
}
fclose(fptr);
}
return 0;
}
Hello I am writing a program that reads the contents of a binary file and prints them to the screen.
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr;
char filename[100];
printf("Enter the filename to open \n");
scanf("%s", filename);
// Open file
fptr = fopen(filename, "rb");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
// Read contents from file
fseek(fptr,0L,SEEK_END);
int fsize = ftell(fptr);
fseek(fptr,0L,SEEK_SET);
unsigned char *c = malloc(fsize);
fread(c,fsize,1,fptr);
fclose(fptr);
printf("%s",c);
return 0;
}
but it does not print anything.Can someone explain me why and how should I fix this problem.
What you have attempted is not at all what you wanted to achieve.
Remember printf() formats the data it prints. To be printed properly with the %s formatting, the binary data values must be ASCII values but , of course, they are not.
You should probably attempt to printf() with %d.
i'm testing the fgetc() function but it doesn't work properly (i have used this function befor so i know how it works)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file = NULL;
int n;
file = fopen("test.txt", "w+");
if(file != NULL)
{
fputs("ab", file);
printf("%c", fgetc(file));
}
else
{
printf("error");
}
return 0;
}
the output should be "a" but it's somthing else
The file is opened for both writing and reading but you need to fseek to the correct place in the file (here, the beginning). In particular, when switching between writing and reading you need to fseek or fflush.
When the "r+", "w+", or "a+" access type is specified, both reading
and writing are enabled (the file is said to be open for "update").
However, when you switch from reading to writing, the input operation
must encounter an EOF marker. If there is no EOF, you must use an
intervening call to a file positioning function. The file positioning
functions are fsetpos, fseek, and rewind. When you switch from writing
to reading, you must use an intervening call to either fflush or to a
file positioning function.
In any case, after writing to the file, the file pointer is in the wrong place to read what was just written.
So the code becomes
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *file = NULL;
file = fopen("test.txt", "w+");
if(file != NULL) {
fputs("ab", file);
fseek(file, 0, SEEK_SET);
printf("%c", fgetc(file));
fclose(file);
}
else {
printf("error");
}
return 0;
}
And if you want to continue writing to the file, you must fseek to its end.
Your error is that you are trying to read a file that has been opened for writting. You should write inside it, then close the file and reopen it for reading. This code will show what I am telling:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fileRead, *fileWrite = NULL;
int n;
fileWrite = fopen("test.txt", "w+");
if(fileWrite != NULL)
{
fputs("ab", fileWrite);
fclose(fileWrite);
}
else
{
printf("error");
}
// Open again the file for read
fileRead = fopen("test.txt", "r");
printf("%c", fgetc(fileRead));
fclose(fileWrite);
// End function
return 0;
}
I have reduced the program with comments, down to this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE * in;
in = fopen(argv[1], "r");
fclose(in);
fprintf(stderr, "clear **** \n");
return(0);
}
yet it still segfaults. Output is:
clear
Segmentation Fault
I know it's fclose(in) that's causing the problem, I just don't know why. For whatever reason, fclose(out) works perfectly fine.
First thing you should do is something like:
in = fopen(argv[1], "r");
if (in == NULL)
printf ("Could not open file '%s'\n" argv[1]);
else
fclose(in);
There's no guarantee in your original code that the fopen is actually working, in which case it will return NULL and the fclose will not be defined behaviour.
i'm new to C programming but am getting the hang of it. I'm working on a FILE function and the function will never return NULL. This happens even when the file does not exist and is initialized as "r". The code has worked before, but after that it has kept returning TRUE. I have written the code multiple ways but all return as the file being there. I have even changed the file name to make a completely different file but still get same results. Any help would be great. Thanks in advance.
EDIT:
Thanks everyone. I've gotten it to work.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
fp = fopen("c:\\lest.txt", "w");
if(fp == NULL)
{
printf("File Not Available\n");
exit(0);
}
fclose(fp);
return 0;
}
You need to be checking fp against null, not fopen.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
fp = fopen("c:\\lest.txt", "r");
if(fp == NULL)
{
printf("File Not Available\n");
exit(0);
}
fclose(fp);
return 0;
}
fp = fopen("c:\\lest.txt", "r");
if(fopen == NULL)
These lines are so wrong. fopen() is a standard function. So the fopen (without parenthesis) will always be non-NULL as it represents the function pointer of fopen().
What you should do is to check the return value of fopen(). Which is in this case fp.
if(fp==NULL){
perror("fopen");
exit(1);
}
Also note,
To print exact error you should use perror() function.
exit(0) will return 0 to OS. Which indicates success. Better use exit(1).
Instead of testing fopen for null, change the conditional to test fp for NULL instead:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE fp*;
fp = fopen("c:\\lest.txt", "r");
if(fp == NULL)
{
printf("File Not Available\n");
exit(0);
}
fclose(fp);
return 0;
}
as u are saying that even if change the file name & then call fp = fopen("c:\lest.txt", "w"); even there is no existing file of name lest.txt, u get fp!=NULL.
it happens because if the specified file is not present then new file with the specified name in the fopen() function is created then it's file pointer is returned so i think u have gotten ur answer.
for more info please see the man page of the function fopen() http://linux.die.net/man/3/fopen