I am trying to create a program to create a text file, write an unknown amount of structures and other files contents into one main file. When I write a structure to a text file, it writes all the empty elements of a character array, and I want to avoid this. Any idea on how to prevent these elements from being written? I am at a beginning part of the program and am working on building it.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct hdr
{
int file_size;
char deleted[1];
char file_name[256];
};
int main()
{
//Open the main file, check if the main header exists
FILE *fp;
int exists = 0;
fp = fopen("CS3411TAR.txt","a+b");
//Check if exists
char* buf[100];
while(fscanf(fp," %*s %*s %s ",buf) >0){
exists = 1;
}
if(exists == 0){
//file header DNE
struct hdr create = {atoi("-10"),"0","CS3411 TAR"};
fwrite( &create, sizeof(struct hdr),1,fp);
}
//To-Do open file arguments names create headers and write
fclose(fp);
return 0;
}
This is the file output, some elements have been removed as it just continues 200+ odd times
öÿÿÿ0CS3411 TAR^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
Im hoping for an output of
-10
0
CS3411 TAR
Please remember that the if
clause needs an expression to evaluate.
Your expression exists = 0
always results in the value of exists
which is 0
.
Correct the if expression to !exists
which is a boolean expression to compare exists
against the value zero.
If exists
has a value not equal to 0
, you write to the file, otherwise you skip the write statement.
That's what you meant.
Add a fread
statement instead of fscanf
to check if the header exists in the file you just opened.
You write binary, so read binary, too.
The real problem appears, when all simple issues are fixed, is: the file opened for appending is positioned at the end of file.
Use fseek
to position the file pointer everytime you want to read the header or a record from the file.
Here ist my working version:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct hdr
{
int file_size;
char deleted;
char file_name[256];
} Header;
const Header defaultHeader = {-10, 0, "CS3411 TAR"};
int main()
{
//Open the main file, check if the main header exists
FILE *fp;
int exists = 0;
fp = fopen("CS3411TAR.txt", "a+");
if (!fp)
{
perror("Couldn't open file");
return EXIT_FAILURE;
}
Header header;
//Check if exists
fseek(fp, 0, SEEK_SET);
int bytesRead = fread(&header, sizeof(Header), 1, fp);
printf("bytesRead: %d\n", bytesRead);
if (bytesRead == 1)
{
printf("Header found!\n");
}
else
{
fseek(fp, 0, SEEK_SET);
//file header DNE
fwrite(&defaultHeader, sizeof(Header), 1, fp);
printf("Header written.\n");
}
//To-Do open file arguments names create headers and write
fclose(fp);
return EXIT_SUCCESS;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.