Copy content of img file to another img file - c

I am trying to make a function that opens 2 files- one for reading and the other one for writing and then copies the first file into the other one binary and for some reason its just does not work. I was trying to debug the program few times but i could not recognize the problem.
void myCopyBinary(char * * argv) {
FILE * srcFile;
FILE * dstFile;
int yesNo = 0;
char temp = ' ';
int i = 0;
int size = 0;
char * buffer;
int resultFread = 0;
int resultFwrite = 0;
srcFile = fopen(argv[1], "rb");
if (srcFile != NULL) {
dstFile = fopen(argv[2], "rb");
if (dstFile != NULL) {
printf("Would you like to overwrite it? (Every Number -YES, 0-NO): ");
scanf("%d", & yesNo);
if (yesNo == 0) {
fclose(dstFile);
exit(0);
return 1;
}
}
dstFile = fopen(argv[2], "wb");
if (dstFile != NULL) {
fseek(srcFile, 0, SEEK_END); // non-portable
size = ftell(srcFile);
buffer = (char * ) malloc(sizeof(char) * size);
if (buffer == NULL) {
printf("Error with the buffer!\n");
exit(1);
}
do {
resultFread = fread(buffer, 1, sizeof(buffer), srcFile);
resultFwrite = fwrite(buffer, 1, resultFread, dstFile);
} while (resultFread > 0);
}
}

One a side note, before reopening the dstFile for writing you should close the previous descriptor.
Anyways, you called fseek(srcFile, 0, SEEK_END);, however you didn't seek back to the beginning, thus when you attempt to read you hit EOF. I guess this is the root of your problem, if not(or rather after fixing this it still doesn't work) then please give more details how it does not work.

Related

Searching a string in a binary file in C

In this code snippet I'm trying to add a string in a binary file. If the string already exists, I return error, otherwise I add it at EOF. I creataed two functions, void AddNewTeam(FILE* fp, char* newTeam), and int SearchTeam(FILE* fp, char* newTeam) as shown below, but that didn't work. Everytime I enter a string, it is added at EOF, even if is in the binary file.
I think the problem is the fread function, I tried to print the value returned by the fread but it is always 0. Can someone help me trying to understand what's wrong with this code and why it's not working?
void AddNewTeam(FILE* fp, const char* newTeam){
int found;
if((fp = fopen("File.dat", "rb")) == NULL){
fp = fopen("File.dat", "ab");
fclose(fp);
}
printf("\tEnter New Team: ");
scanf("%s", newTeam);
found = SearchTeam(fp, newTeam);
if(found == 0){
if((fp = fopen("File.dat", "ab"))){
fwrite(newTeam, sizeof(newTeam), 1, fp);
printf("\tThe following team has been successfully loaded\n");
fclose(fp);
}
}else if(found == 1){
printf("\tThis team already exists\n");
exit(EXIT_FAILURE);
}
}
int SearchTeam(FILE* fp, const char* newTeam){
char string[MAX][MAX_LENGTH];
int counter, result, found = 0;
if((fp = fopen("File.dat", "rb"))){
fseek(fp, 0, SEEK_SET);
for(counter = 0; !feof(fp); counter++){
if(fread(string[counter], sizeof(string[counter]), 1, fp) == 1){
result = strcmp(newTeam, string[counter]);
if(result == 0){
found = 1; break;
}else if(result != 0){
found = 0;
}
}
}
fclose(fp);
return found;
}else if(fp == NULL){
printf("\tError opening binary file\n");
exit(EXIT_FAILURE);
}
}
This is the main function and the function prototypes
int SearchTeam(FILE* fp, const char* newTeam);
void AddNewTeam(FILE* fp, const char* newTeam);
int main(void){
FILE* fp;
char newTeam[MAX_LENGTH];
AddNewTeam(fp, newTeam);
return 0;
}
You're not appending to the file correctly. The search code expects each team name to be in a block of MAX_LENGTH bytes. But when you write, you do:
fwrite(newTeam, sizeof(newTeam), 1, fp);
newTeam is a pointer, not an array, so sizeof will be the size of a pointer, typically 4 or 8 bytes. You need to write MAX_LENGTH bytes so this will match the way you read from the file.
fwrite(newTeam, MAX_SIZE, 1, fp);

Lines containing given word in output file C

I am very new to working with files and I can't seem to get my head around this. What I am trying to do is to write in the Exit.txt file all the lines that have my given word in them. For example, if my word is "exercise" and my In.txt contains the following:
I exercise daily
I like apples
How often do you exercise
I am tired
Then in Exit.txt I should have
I exercise daily
How often do you exercise
The problem is that somehow it only writes the last line in the Exit.txt file, and sometimes it doesn't even write anything, depending on my input In.txt.
I would very much appreciate any help, thank you very much!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE* fis, * fis2;
char* sir, * rez, word[50];
printf("Word: ");
gets(word);
sir = malloc(50 * sizeof(char));
fis = fopen("In.txt", "rt");
if (fis == NULL)
printf("Can't open file!");
else
{
while (!feof(fis))
{
rez = fgets(sir, 50, fis);
if (strcmp(rez,word)==0)
{
fis2 = fopen("Exit.txt", "wt");
fputs(sir, fis2);
}
}
}
fclose(fis);
free(sir);
return 0;
}
When you open a file for writing in a loop you must know about the offset. Its better open file in append mode, write your data and close it.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE* fis, * fis2;
char* sir, * rez, word[50];
char *line = NULL;
ssize_t bufsiz = 0;
ssize_t nbytes;
printf("Word: ");
gets(word);
fis = fopen("In.txt", "rt");
if (fis == NULL)
printf("Can't open file!");
else
{
while ((nbytes = getline(&line, &bufsiz, fis)) != -1)
{
char * ptr_value = strstr(line,word);
if(ptr_value != NULL) {
printf(line);
fis2 = fopen("Exit.txt", "a");
fputs(line, fis2);
fclose(fis2);
}
}
}
fclose(fis);
return 0;
}
there are a few changes needed to get your code working:
only open the output file once (so not in the while loop)
to check if a string is a part of another string please use "strstr" and not "strcmp"
don't forget to close your file at the end
So here is a suggestion for a solution
int main()
{
FILE* fis, * fis2;
char* sir, * rez, word[50];
printf("Word: ");
gets(word);
sir = malloc(50 * sizeof(char));
fis = fopen("In.txt", "rt");
if (fis == NULL)
printf("Can't open file!");
if ((fis2 = fopen("Exit.txt","wt"))==NULL){
printf("Cant't open Exit-file\n");
return EXIT_FAILURE;
}
else
{
while (!feof(fis))
{
rez = fgets(sir, 50, fis);
if(strstr(rez,word)!=NULL)
{
fputs(sir, fis2);
}
}
}
fclose(fis);
fclose(fis2);
free(sir);
return 0;
}

How to write CSV fields on a binary file?

I have the following code:
typedef struct RegDados{
char removido; // deve ser inicializado com '-'
int encadeamento; // deve ser inicializado com -1
int nroInscricao; // nao aceita valores repetidos nem nulos
double nota;
char data[10]; // checar tipo
char *cidade;
int sizecid;
char tagCampo4;
char *nomeEscola;
int sizesch;
char tagCampo5;
}RegDados;
char * strtokEvenEmpty(char * s, const char * seps){
static char * p = NULL;
if (s != NULL)
p = s;
else if (p == NULL)
return NULL;
else
s = p;
while (*p) {
if (strchr(seps, *p)) {
*p++ = 0;
return s;
}
p += 1;
}
return (*s) ? s : NULL;
}
const char * getfield(char* line, int num){
const char * tok;
for (tok = strtokEvenEmpty(line, ","); tok; tok = strtokEvenEmpty(NULL, ",\n")){
if (!--num)
return tok;
}
return NULL;
}
int main(){
FILE * stream = fopen("trabalho1.csv.csv", "r+");
FILE * ArqBin = fopen("test.bin","wb");
RegDados regdados[5000];
RegCab regcab;
int i = 0;
if(ArqBin == NULL) printf("Error");
if (stream != NULL) {
char line[1024];
while (fgets(line, 1024, stream)) {
regdados[i].nroInscricao = atoi(getfield(line, 1));
fwrite(&regdados[i].nroInscricao, sizeof(int), 1, ArqBin);
regdados[i].nota = atof(getfield(line, 2));
fwrite(&regdados[i].nota, sizeof(double), 1, ArqBin);
strcpy(regdados[i].data, getfield(line, 3));
fwrite(regdados[i].data, sizeof(char), 100, ArqBin);
regdados[i].cidade = getfield(line, 4);
fwrite(regdados[i].cidade, sizeof(char), 100, ArqBin);
regdados[i].nomeEscola = getfield(line, 5);
fwrite(regdados[i].nomeEscola, sizeof(char), 100, ArqBin);
i++;
}
fclose(stream);
fclose(ArqBin);
}
else{
printf("Error");
}
}
It already parses the fields of my file, but I can't write them on a binary file, because when I try to write, I get a lot of null fields, which doesn't happen when I don't write.
My CSV file looks like:
nroInscricao,nota,data,cidade,nomeEscola
13893,353.9,26/11/2016,,FRANCISCO RIBEIRO CARRIL
13595,472.2,,Salgueiro,ALFREDO GUEDES
13894,614.4,28/11/2016,Recife,JOAO DE MOURA GUIMARAES
13880,403.2,29/11/2016,Fortaleza,ANTONIO DIAS PASCHOAL PR
13881,373.7,,Sao Jose da Tapera,DONIZETTI TAVARES DE LIM
13882,394.8,01/12/2016,Sao Bernardo do Cam,JUSTINO GOMES DE CASTRO
How can I write the each field on a binary file?
when I try to write, I get a lot of null fields, which doesn't happen when I don't write.
this is normal, the internal representation of number can contains several 0, for instance doing :
fwrite(&regdados[i].nroInscricao, sizeof(int), 1, ArqBin);
if regdados[i].nroInscricao values 7 and your int are on 32bits that will write 3 times 0 and 1 time 7 (the order depends if you are on little/big endian).
Of course it is the same with the strings you write with a fixed size, so the padding characters can have any value including 0 (there are not initialized)
Your way to extract the fields with getfield is expensive because you extract the first token, then to get the second you have to bypass the first token, then to get the third you have to bypass the 2 first tokens etc.
A better way is to do getfield(line, 1) then getfield(NULL, 1) to get the second token, then to do getfield(NULL, 1) to get the third etc, so in fact the second argument is always 1 and you can remove its management
You try to open trabalho1.csv.csv, probably you want to open trabalho1.csv
In
if(ArqBin == NULL) printf("Error");
if (stream != NULL) {
it is not enough to print error, you must not continue, can be
if(ArqBin == NULL)
printf("Error");
else if (stream != NULL) {
or better replace
FILE * stream = fopen("trabalho1.csv.csv", "r+");
FILE * ArqBin = fopen("test.bin","wb");
...
if(ArqBin == NULL) printf("Error");
if (stream != NULL) {
...
}
else{
printf("Error");
}
by something like
FILE * stream = fopen("trabalho1.csv.csv", "r+");
if (stream == NULL) {
fprintf(stderr, "cannot open input file rabalho1.csv.csv");
return -1;
}
FILE * ArqBin = fopen("test.bin","wb");
if (ArqBin == NULL) {
fprintf(stderr, "cannot open output file test.bin");
fclose(stream); /* really useful if you do not stop execution */
return -1;
}
...

Generating array of word pointers in c

I have a problem where I have to read a text file made of 264064 words into a buffer and then create an array of word-pointers in a separate array. I am not sure how to go about creating the array of word-pointers which points to different amount of characters in the buffer. Any hints on how to approach this problem?
#include <stdlib.h>
#include <string.h>
int main()
{
int i,wordCount=0;
long bufsize;
char ch;
//Open File and get number of lines in file
FILE *fp = fopen("words2.txt", "r");
if (fp == NULL) {
printf("Error!");
exit(1);
}
do {
ch = fgetc(fp);
if (ch == '\n')
{
wordCount++;
}
} while (ch != EOF);
fclose(fp);
printf("%d\n",wordCount);
//Reading Words into buffer rawtext
char *rawtext;
fp = fopen("words2.txt", "rb");
if (fp != NULL)
{
if (fseek(fp, 0L, SEEK_END) == 0) {
bufsize = ftell(fp);
if (bufsize == -1) {
exit(1);
}
rawtext = malloc(sizeof(char) * (bufsize + 1));
if (fseek(fp, 0L, SEEK_SET) != 0) { exit(1); }
size_t newLen = fread(rawtext, sizeof(char), bufsize, fp);
if (ferror(fp) != 0) {
fputs("Error reading file", stderr);
} else {
rawtext[newLen++] = '\0';
}
}
//Print out buffer
printf("%s",rawtext);
fclose(fp);
free(rawtext);//Free allocated memory
char *ptr[wordCount];//Array for word-pointers
}
}
If you keep your rawtext (i.e. do not free it), you could use strchr('\n') to go through the content, store to the array the current position, detect every new line char, terminate the string at this new line character, and go ahead. Thereby, your ptr-array will point to each word inside rawtext at the end (that's why you should not free rawtext then, because the pointers would then point to invalid memory):
The following code should work:
char* currWord = rawtext;
int nrOfWords = 0;
char* newlinePos;
while ((newlinePos = strchr(currWord,'\n')) != NULL) {
*newlinePos = '\0';
ptr[nrOfWords++] = currWord;
currWord = newlinePos + 1;
}
if (*currWord) {
ptr[nrOfWords++] = currWord;
}
Side note: expression char *ptr[wordCount] might put your pointer array on the stack, which has limited space, at least less than the heap. This could get a problem if your file contains a lot of words. Use char *ptr = malloc((wordCount+1) * sizeof(char*)) to reserve memory on the heap. Note also the +1 after wordCount for the case that the last word is not terminated by a new line.

C: Too many files open

This code opens a directory, and for every file in the directory it loops through every line of data inside the file, and then parses it to do some calculations and outputs the resulting data into a new file.
The problem is that I can only output a maximum of around 1021 files. I'm closing all of the fopens after outputting all the data, so I'm not sure what I'm doing wrong.
Shouldn't fclose() be closing the open files therefore this not happening?
int main(int argc, char *argv[])
{
//sample data values
double lat;
double lon;
double convergence;
double pt_scale;
int zone = 54;
double major_axis = 6378137.0000;
double flattening = (1/298.2572);
double zoneWidth = 6;
double centMeridian = -177;
double falseEast = FALSE_EASTING;
double falseNorth = FALSE_NORTHING;
double scale = SCALE_FACTOR;
int max_size = 128;
int current_size = max_size;
char *pathStr = malloc(max_size);
char *outPathStr = malloc(max_size);
char coData[100]; //max length of line;
long firstTerm, secondTerm; //terms we will split the line into, lat, lon, elevation.
int counter = 0; //pos counter
int d = EOF; //end of file ASCII
char strIn[200];
char* elevation;
char strOut[200];
char dirOut[200]; //sprintf must use a actual defined buffer otherwise there will be a buffer overflow.
char* cchr;
int j;
_setmaxstdio(2048);
printf("Please enter the path of the files: \n");
getUserInput(pathStr, current_size, max_size);
printf("Please enter the output path of the files: \n");
getUserInput(outPathStr, current_size, max_size);
//loop through each file in the directory. Open the file, convert, then close it.
//we will use dirent.h as it is cross platform so we wont have to worry about sharing issues
DIR *dir; //new directory
struct dirent *ent;
dir = opendir(pathStr); //allcate it a path
if(opendir(pathStr) == NULL)
{ printf("Error: %d (%s)\n", errno, strerror(errno));}
int k;
if(dir != NULL)
{
while((ent = readdir(dir)) != NULL) //loop through each file in the directory.
{
//open the file and loop through each line converting it then outputing it into a new file
if((!strcmp(ent->d_name,"..") || !strcmp(ent->d_name,".")) == 1)
{
//dont want these directories
continue;
}
else
{
sprintf(strIn,"%s%s",pathStr,ent->d_name); //get the file n
FILE *fp = fopen(strIn, "r");
if(fopen(strIn, "r") == NULL) //for inputting file
{ printf("Error: %d (%s)\n", errno, strerror(errno));
getchar();
break; }
sprintf(dirOut,"%s%d%s",outPathStr,counter,".geo");
printf("%s \n",dirOut);
FILE *fp2 = fopen(dirOut, "w"); //for outputting file
if(fopen(dirOut, "w") == NULL)
{ printf("Error: %d (%s)\n", errno, strerror(errno));
getchar();
break; }
while(fgets(coData, 100, fp) != NULL)//loop through line by line, allocate into 2 doubles and a string, pass the two coordinates and convert
{
//extract terms from coData
char * pch; //pointer to array pos
char * pend;
pch = strtok(coData," ");
j = 0;
while(j <= 2) //We only want to split the first three parameters.
{
//convert char array to double for co-oridinate conversion
if(j == 0)
{
firstTerm = atof(pch); //latitude;
j++;
continue;
}
if(j == 1)
{
pch = strtok(NULL, " ");
secondTerm = atof(pch); //longitude
j++;
continue;
}
if(j == 2)
{
pch = strtok(NULL," ");
elevation = pch; //elevation doesnt need to be converted because it isnt used in the coordinate conversion.
break;
}
}
grid2spheroid(&lat,&lon,&convergence,&pt_scale,firstTerm,secondTerm,zone,0, major_axis,flattening,zoneWidth,centMeridian,falseEast,falseNorth,scale);
sprintf(strOut,"%f %f %s",lat,lon,elevation);
//printf("%d %d", lat, lon);
fputs(strOut,fp2);
} //end of while
fclose(fp2);
fclose(fp);
counter++;
}
}
closedir(dir);
}
free(pathStr); //finished using the path string so we can finish the
free(outPathStr);
getchar();
return 0;
}
void getUserInput(char *pathStr, int current_size, int max_size)
{
unsigned int i = 0;
if(pathStr != NULL)
{
int c = EOF;
//get the user input and reallocate the memory size if the input it too large.
while((c = getchar()) != '\n' && c != EOF) //WHILE NOT END OF FILE OR NEW LINE (USER PRESSED ENTER)
{
pathStr[i++] = (char)c;
if(i == current_size)
{
current_size = i+max_size;
pathStr = realloc(pathStr, current_size);
}
}
}
}
You aren't closing all the files ;-)
FILE *fp = fopen(strIn, "r");
if(fopen(strIn, "r") == NULL) //for inputting file
Same applies to your output.
I think you meant something more like:
FILE *fp = fopen(strIn, "r");
if(fp == NULL) //for inputting file
{
// error handling.
No, no! You're opening every file twice (and only closing once)!
/* Bad! */
dir = opendir(pathStr); //allcate it a path
if(opendir(pathStr) == NULL)
{ printf("Error: %d (%s)\n", errno, strerror(errno));}
int k;
/* Correct */
dir = opendir(pathStr); //allocate it a path
if(!dir) {
printf("Error: %d (%s)\n", errno, strerror(errno));
return;
}
You're also doing the same thing with fopen(). In both places :)
Just check the pointer; don't call "fopen()" a second time; don't call "opendir()" a second time!
Also: please don't put code on the same line as your opening brace. OK?
dir = opendir(pathStr); //allcate it a path
if(opendir(pathStr) == NULL)
(...)
FILE *fp2 = fopen(dirOut, "w"); //for outputting file
if(fopen(dirOut, "w") == NULL)
(...)
FILE *fp = fopen(strIn, "r");
if(fopen(strIn, "r") == NULL) //for inputting file
Here you open the file twice but only store the pointer once. Change these to:
FILE *fp = fopen(strIn, "r");
if(fp == NULL) //for inputting file
and the other one in the same way.

Resources