C fopen/fwrite driving me to madness - fwrite not writing - c

Ok, here is the deal...i remember creating a program past week that required me to open a file in binary mode and write data to it. At first, i tried using the fopen function, checking if the result is ok, then try to write data. I remember that at first try, the fwrite operation wasn't working. Then, after moving declaration of variables from a place to another, i was finally able to make the fwrite to insert data to the file.
Now, i need to create another similar program to do some other stuff, so i wanted to use the same allocation code (actually, i wanted to create a specific function to do the same), and here is what i was able to come up with:
#include <stdio.h>
int openfile(FILE *main, char *name, int option);
int main(void)
{
FILE *main;
int header_init;
int result;
switch (openfile(main,"main_file.bin",1)) {
case 1:
header_init = -1;
//fseek(main,0,SEEK_SET); --> useless
fwrite(&header_init,sizeof(int),1,main);
printf("ftell = %d\n",ftell(main)); break;
case 2:
fread(&header_init,sizeof(int),1,main);
printf("%d\n",header_init); break;
default:
printf("Error trying to open file\n");
}
printf("header_init is %d\n",header_init);
fclose(main); exit(0);
}
int openfile(FILE *main, char *name, int option)
{
int result_alloc;
int F_OK;
if (result_alloc = access (name, F_OK) != 0) {
printf("File not found, allocating a new one\n");
if ((main= fopen(name,"w+b")) != NULL) return 1;
}
else {
printf("File exist, allocating as r+b\n");
if ((main= fopen(name,"r+b")) != NULL) return 2;
}
printf("Failure trying to open");
return 0;
}
For some unfortunate reason, the fwrite operation is not writing -1 to the allocated file. My intention with this program is so that it will always check for existence of that specific file: if there is one in place, simply open it with r+b to allow update functions without overwriting the actual file contents. Otherwise, allocate a new one with a header value of -1 (i will use this file as a record file with chained list structure).
Seriously, i cannot understand why this is not working. The idea is the same of my previous program. The only thing that changed is that i created a function, because this is going to happen me later (because of the 3rd parameter that will allow me to reduce my code and make it more "readable" - at least this is the intention!). I HAVE to admit that i have some attention to details problem, but i am working hard to get better at it, i am probably missing something stupid in this code, but after hours looking at it i really wanted to ask here for some help. Thanks
Edit: I am running it under z/Linux. What i am trying to understand is, why the code above doesn't write -1 to the file, but the one below writes ok?
#include <stdio.h>
int main(void)
{
FILE *main;
int result_alloc;
int header_init;
int F_OK;
if (result_alloc = access ("test.bin", F_OK) != 0) {
printf("File not found, allocating a new one\n");
if ((main = fopen("test.bin","w+b")) == NULL) {
printf("Failure trying to open file");
return 1;
}
else {
header_init = -1;
printf("current pos is: w+b %d\n",ftell(main));
fwrite(&header_init,sizeof(int),1,main);
printf("current pos is: write header_init %d\n",ftell(main));
}
}
else {
if ((main = fopen("test.bin","r+b")) == NULL) {
printf("Failure trying to open file");
return 2;
}
else {
printf("current pos is: r+b %d\n",ftell(main));
fread(&header_init,sizeof(int),1,main);
printf("current pos is: read header_init %d\n",ftell(main));
}
}
}

The main issue that the assignment to the main variable inside of the openfile function is not seen by the calling function. Because C is pass by value, you're only changing the value of the function parameter, not the value of the variable that was passed in. So when openfile returns, the main variable inside of the main function is unchanged.
What you need to do is pass the address of that variable to the function, then within the function you dereference the local variable (which is a pointer) and assign a value to the dereferenced variable.
Also, it's not a good idea to have a variable with the same name as a function as it hides the function in that scope and can cause confusion.
So you would define your function as follows:
int openfile(FILE **fp, char *name, int option);
You would then call it like this (changing the name of the main variable to fp):
FILE *fp;
...
openfile(&fp,"main_file.bin",1)
Then inside of openfile, you dereference fp to change the value in the calling function:
*fp = fopen(name,"w+b")
The reason why the second code sample is working is that you're assigning directly to a local variable and then using that same variable later on in the function.
Also, you're "lucky" that the second piece of code is working because you did this:
int F_OK;
F_OK is already defined in unistd.h, where access() is defined. So by doing this you're redeclaring it and not giving it a value, causing undefined behavior. Get rid of this definition, and #include <unistd.h>, and the call to access() is guaranteed to work as expected.

Related

Problems with if ....==NULL in C

I have this code, note that it is shortened down. The problem is if the file exists
it still overwrites it. Been 30 years since I did any programming so bear with me. Thanks!
FILE *openFil(FILE *open, char namn[]);
int main(int argc, const char * argv[])
{
FILE *openFil(FILE *open, char namn[]);
FILE *anmal =NULL;
char filNamn[] = "ANMAL.DAT";
postTypAnm pAnm;
anmal = openFil(anmal, filNamn);
}
FILE *openFil(FILE *pointer, char name[])
{
if ((pointer =fopen(name, "r+b"))== NULL)
if ((pointer =fopen(name, "w+b"))== NULL)
{
/* It Enters here as well, but it should not do that or????? */
printf("error\n");
exit(0);
}
return pointer;
}
If you're using the C11 standard you can use the "x" argument to specify that if the file exists the fopen() function will fail.
For reference: http://www.cplusplus.com/reference/cstdio/fopen/
Here's a working example.
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
bool openFile(FILE **ptr,
const char *fileName);
int main(int argc, const char * argv[]) {
FILE *anmal = NULL;
const char *fileName = "ANMAL.DAT";
if(!openFile(&anmal, fileName)) {
exit(EXIT_FAILURE);
}
}
bool openFile(FILE **ptr,
const char *fileName)
{
*ptr = fopen(fileName, "w+bx");
if(!*ptr) {
perror("Error opening file: ");
return false;
}
return true;
}
This is using the x extension in GNU C to test whether the file exists.
As other people have pointed out, there are numerous problems in your original code.
You've redeclared the function prototype for openFil within main.
In openFil there's no point in both accepting the FILE pointer as
a parameter and overwriting it with the return value. Especially if
you're expecting to input a NULL pointer and use the function to
initialise it. Either use a pointer-to-pointer as a parameter so you
can modify the pointer within the function, or ignore it completely
and set its value with the function's return value. Not both.
You're not actually testing for whether the file exists at all. According to the manual pages for fopen (man fopen) neither of the flags you used in opening the file (r+ and w+) care whether the file exists. r+ opens for reading/writing and always positions the stream at the beginning of the file. w+ opens for reading/writing, truncating the file if it exists already. This explains why you didn't get the effect you intended.
1.
"It Enters here as well, but it should not do that or?????"
No, It shouldn´t. If both pointers are NULL the opening of the streams to ANMAL.DAT were not successful, neither in w+b nor in r+b mode. Proof if the file really exist in the directory of the executable. Else try to use the entire path from the root directory to the file.
"The problem is if the file exists it still overwrites it."
Why do you know that the file is really overwritten in a proper manner?
Since if ((pointer =fopen(name, "r+b")) == NULL) and if ((pointer = fopen(name, "w+b"))== NULL) both fail, it seems that the ANMAL.DAT does not exist where fopen() searches for it or otherwise an error occurs when trying to open it (maybe has incorrect format or is corrupted?).
Place perror(name) in the error routine to check if errno was set to indicate an error at name.
2.
In the error routine: exit(0) is not correct if an error has happened. Use exit(EXIT_FAILURE).
Side note:
You have another prototype for the function openFil-> FILE *openFil(FILE *open, char namn[]); inside of main, which is redundant.
Also the identifier of the second parameter is different in the prototype before main to the identifier at the definition of openFil, namn in comparison to name.

Pipe's write overwrites an allocated space of memory

My program it's pretty big, so I'll highlight the main problem and add some details about it.
First part of my code:
int myPipe[2]; //A global variable, so I don't have to pass it to future functions
int main(int argc, char *args[])
{
mode_t pUmask = umask(0000); //Obsolete variable to my problem
errno = 0; //Obsolete variable to my problem
char pattern[2500] = "Test1"; //Obsolete variable to my problem
int p = 0; //DEFAULT NUMBER OF PROCESSES
int deep = 0; //Obsolete variable to my problem
int n = 1; //Obsolete variable to my problem
if(pipe(myPipe))
{
perror("Pipe Error: ");
exit(-1);
}
if( (write(myPipe[1], &p, (sizeof(int)*3))) == -1) //First write works
{
perror("write: ");
exit(-1);
}
//Then a bunch of code releated to file reading
}
Second part:
{
//in another function
//The part where I create fileName
char* fileName = calloc(strlen(fileData->d_name)+4, sizeof(char));
strcpy(fileName, fileData->d_name);
}
Third part:
//in another another function
if(S_ISREG(data.st_mode))
{
printf("\tfileName: %s\n", fileName); //Regular print of the right fileName
printf("\t\tOh boy! It's a regular.\n");
printf("\tfileName: %s\n", fileName); //Regular print of the right fileName
if((read(myPipe[0], &p, (sizeof(int)*3))) == -1) //First time I read
{
perror("\t\t read: ");
exit(-1);
}
printf("fileName: %s", fileName); //SEGMENTATION FAULT
There is a bunch of code in between, but it doesn't affect the fileName at all (in fact, up until the "read", fileName was printed flawlessly), and after it a SEGMENTATION FAULT happens.
At one point by changing the printfs locations I was able to get the fileName AFTER the read, which was basically the fileName value("File1") followed by the p integer value(0), which created the new corrupted fileName("File10").
So what's happening? I reserved the space for fileName, I passed the fileName pointer to the following functions up to that read, and supposedly the fd should have it's own adress space as well. HELP.
P.s. if you need more info, I'm willing to give it to you, even the full code, but it's REALLY complicated, and I think I gave you enough proof that fileName doesn't get corrupted at all until the read part, THANK YOU.
P.p.s.
I never close either of the "MyPipe" extremes, since I have to use them multiple times, I wanted to close them at the end of the program.
The statements that write and read the pipe are causing undefined behavior. p is declared:
int p;
But when you write and read it through the pipe, you use sizeof(int)*3, so you're accessing outside the object.
Change those statements to use just sizeof p.

How to use goto across multiple functions

I am trying to exit a program without using exit(). I have come up with a very convoluted and dirty solution (I am a Beginner).
I would like to use if statements and if it is true, then I would like to use goto to go the main function and then return 3; and end the program.
Here is a bit of code:
FILE *filepointer;
char * line = NULL;
size_t len = 0;
size_t read;
int linecount = 0;
filepointer = fopen(filename, "r");
if (filepointer == NULL)
{
printf("[ERR] Could not read file %s.\n",filename );
goto FILE_ERROR;
}
...
int main(){
...
FILE_ERROR: return 3;
}
This however does not work as I cannot get jump between functions because I get undeclared Label as an error. Is there any way I can exclude exit() from my program and still end it returning a certain value. If there is a better solution, please let me know
The only good answer to this question is: don't do it. gotos used in this way make your code very hard to reason about.
Refactor your code so that you have a clear structure and hierarchy of calls. Use return values to propagate success/failure throughout the call stack.
goto can't be used to jump across different functions; it can only be used within the same function. To jump between functions, you can look at setjmp() and longjmp() functions.
Having said, since you claim to be a beginner, I am not convinced you really need to use either of the above. You can simply modify your function to return an "error" value. And in main(), check its value and return from main() with the error value.
By design, a goto cannot jump from one function to another. It can only be used to jump within a given function.
There are ways to jump between functions, but doing so is not only very poor design but also dangerous as it is very easy to put your program in an invalid state.
The proper way to handle this is to have your function return a specific value (or set of values) to indicate an error. Then the calling function would check for one of those error values and act accordingly.
For example:
int readFile(char *filename)
{
FILE *filepointer;
char * line = NULL;
size_t len = 0;
size_t read;
int linecount = 0;
filepointer = fopen(filename, "r");
if (filepointer == NULL)
{
// add strerror(error) to the error message to know why fopen failed
printf("[ERR] Could not read file %s: %s.\n",filename, strerror(errno) );
// error completion
return 0;
}
...
// successful completion
return 1;
}
int main(){
...
if (readFile("myfile") == 0) {
return 3;
}
...
}
If you wanted to use a go-to , and insisted on doing that, you could I guess try to expand your 1st function so it includes / encapsulates the 2nd function, and get rid of the 2nd function conpletely, so your able to do go tos and subroutines within this much larger function.
Is that an option you could try ? (If you were dead cert on using Goto's, ) ?
I would give that a go.

How to create function that opens files and let's other functions use them? [duplicate]

This question already has answers here:
How to change value of variable passed as argument?
(4 answers)
Closed 5 years ago.
I want to create the function that opens a file and then other functions use this opened file. This is my code,
#include <stdio.h>
int openFile(FILE* inputFile)
{
inputFile = fopen("input.txt", "r");
if (inputFile != NULL)
return 0;
else
return -1;
}
void readWholeFile(FILE* inputFile)
{
char str[20];
while (feof(inputFile)) {
fscanf(inputFile, str);
printf("%s\n", str);
}
}
int main() {
FILE* inputFile;
if (openFile(inputFile) == 0) {
readWholeFile(inputFile);
}
else
printf("File didn't open");
fclose(inputFile);
return 0;
}
"File didn't open" is not printed so the file should be opened but actually readWholeFile prints nothing as a file would be empty. What's the problem?
Your prototype makes no sense, openFile() can't change the caller's FILE * when it's passed by value, you need to pass the address of the pointer in that case:
int openFile(FILE **inputFile)
{
*inputFile = fopen("input.txt", "rt");
return *inputFile == NULL ? -1 : 0;
}
But of course this serves very little purpose, just use fopen() directly where you want to open a file, instead. Returning the pointer to the open file is easier to work with, instead of having to manage a separate int that carries no added value or information (an int being 0 or -1 is not "better" than a pointer being NULL or not NULL).
You should return inputFile itself. That wauy you can reused it from other function.
Also why pass that FILE* to openFile function? It's redundant.
Design-wise you can pass the filename and the parameters like r,w etc.
FILE* openFile(const char*name, const char*params)
{
inputFile = fopen(name, params);
if (inputFile != NULL)
return inputFile;
else
return NULL;
}
But in doing that you are just abstracting out the fopen() call. You still have to check the return value of openFile(). You can use double pointer and achieve the same thing but yes I have provided with an alternative.
FILE *inputFile = openFile("input.txt","r");
if( !inputFile ){
//...
}
The thing is as far as the method shown this is really doing anything other than wrapping the fopen() call. What you can get to know from this answer that you can pass the pointer around in Functions and how to use it.
Other answer provides how you use the double pointer but do you get what happened in previous case?
You are changing the local variable that is passed to openFile(). You change it. And when the function completes then that local variable is not there anymore. It's value wont affect the FILE* variable in main().
To be clear you don't need this method to open a file. It's better if you use the 2 lines alone. Because there is no improvement whatsoever. You still have to check the return value just like you would have in case of direct fopen call.

Preserving file pointer between function calls

I have a code where I'm accessing a binary file several times. Each time I call the function, it opens the file for reading and it reads out only the required number of bytes (say n bytes each time).
The binary file contains time series data, and what I'd like it to be able to do is run the function call through a loop, and every time I call the function to open the same file, it needs to read out the next chunk of data, i.e, I don't want the file pointer to be reset every time. Is there a way to do this?
The function looks like so:
int readBinary(float *binImage, int gelements) {
imageFile = fopen("tmpImageFile", "r");
if (imageFile == NULL) {
fprintf(stderr, "Error opening file\n");
return (1);
}
fread(binImage, sizeof(float), gelements, imageFile);
return 0;
}
And in my main code, I'd like to run it through a loop, giving it the array binImage of size gelements every time. I'd rather not give it an array of size gelements * nLoop if that's avoidable.
The quick rules of thumb are
"Life gets easier if you properly separate responsibilities between functions"
and
"Life gets complicated if you use static or global variables".
In this case, giving the readBinary function both the responsibility of managing the FILE and reading the data is too much.
Note that the function doesn't close the handle.
If the handle is a local static in the function then it will be impossible to close it.
It also means that the function will be forever locked into using only "tmpImageFile" (which is not immediately apparent from the function's signature or missing documentation)
If the handle is a global then it may be possible to close it prematurely.
Notice that if you remove the "open file" responsibility the readBinary function is just a call to fread.
The best way to handle this is to skip the readBinary function altogether.
You say you have a caller which reads data in a loop.
Make this caller responsible for opening the FILE, use fread directly in the loop and close the file when you are done.
Now, this might give the caller too much responsibility. Simply have the caller accept the FILE* as a parameter and give the file management responsibility to its caller.
Or the caller's caller's caller, depending on where it makes sense to manage the file's lifetime.
Use a static variable so you retain the file pointer:
int readBinary(float *binImage, int gelements) {
static FILE *imageFile = NULL;
if (imageFile == NULL) {
imageFile = fopen("tmpImageFile", "r");
if (imageFile == NULL) {
perror("Error opening file: ");
return (1);
}
}
fread(binImage, sizeof(float), gelements, imageFile);
return 0;
}
I would pass the FILE* as parameter to function:
int readBinary(float *binImage, int gelements, FILE *imageFile) {
int bytes = fread(binImage, sizeof(float), gelements, imageFile);
return bytes != -1 ? 0 : 1;
}
I also added simple check for fread return value and converting it to your return value convention. Though this function seems so simple, you could just call fread directly, unless you want to add stuff like error print to it.
You can make the file pointer static and initialize it to NULL. Then next time onwards if it is not NULL, then it contains the opened file. Also while closing the file, make sure you make it to NULL again. Same can be done with a global pointer also
int readBinary(float *binImage, int gelements) {
static imageFile = NULL;
if(imageFile == NULL ) imageFile = fopen("tmpImageFile", "r");
if (imageFile == NULL) {
fprintf(stderr, "Error opening file\n");
return (1);
}
fread(binImage, sizeof(float), gelements, imageFile);
return 0;
}

Resources