I am working on a management system project and want to clear the file before adding data to it. I am using this code as a reference. I have rewritten the code from the reference and instead of writing the data from the temporary file(tmp)
back to the original(FILE_NAME)
, I have printed it out to the terminal.
When I compile and run the program, it prints all the content and a few more lines after the end of the file. After this it stops and doesn't finish execution. I have added to comments to help understand my thought process better.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define BUFFER_SIZE 1000
#define FILE_NAME "data.csv"
int main()
{
FILE* file;
char buffer[BUFFER_SIZE];
// Opening file
if(file = fopen(FILE_NAME, "r+"))
{
char c; // To get character from buffer
int i = 0; // Index for the buffer character
int isEmpty = 1; // If the line is empty
FILE* tmp;
if(tmp = tmpfile())
{
while(1)
{
buffer[i++] = c;
if(c != '
') // Checking for blank lines
{
isEmpty = 0;
}
else
{
if(c == '
' && isEmpty == 0) // Read a word; Print to tmp file
{
buffer[i] = '';
fprintf(tmp, "%s", buffer);
i = 0;
isEmpty = 1;
}
else if(c == '
' && isEmpty == 1) // NOT SURE WHY THIS IS IMPORTANT
{
buffer[i] = '';
i = 0;
isEmpty = 1;
}
}
if(c == EOF)
{ break; }
while(1) // Loop to print contents of tmp file onto terminal
{
c = getc(tmp);
printf("c: %c", c);
if(c == EOF)
{ break; }
}
}
}
else
{
printf("Unable to open temporary file
");
}
fclose(file);
}
else
{
printf("Unable to open file.");
}
getchar();
return 0;
}
UPDATE:
I've modified a few lines and have got it working.
I'd forgotten to assign c in the above program. Also @Barmar won't char c work just as well as int c. Characters can be integers as well right?
Why would large indentations lead to bugs? I find the blocks of code to be more differetiated.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define BUFFER_SIZE 1000
#define FILE_NAME "data.csv"
int main()
{
// Variable Declaration
FILE* file;
char buffer[BUFFER_SIZE];
// Opening file
if( file = fopen(FILE_NAME, "r+") )
{
char c; // Reading characters from the file
int i; // Index of the characters
int isEmpty = 1; // 1-> character is empty; 0-> character is not empty
FILE* tmp;
if( tmp = fopen("tmp.csv", "a+") )
{
char c; // Reading characters from files
int i = 0; // Index
int isEmpty = 1; // 1->previous word is empty; 0->previous word is not empty
while( (c = getc(file)) != EOF)
{
if( c != '
' && c != ' ' && c != '' && c != ',')
{
isEmpty = 0;
buffer[i++] = c;
}
else
{
if( c == '
' && isEmpty == 0 )
{
buffer[i] = '';
fprintf(tmp, "%s", buffer);
i = 0;
isEmpty = 1;
}
else if( c == '
' && isEmpty == 1 )
{
buffer[i] = '';
i = 0;
}
}
}
fclose(tmp);
}
else
{
printf("Unable to open temporary file
");
}
fclose(file);
}
else
{
printf("Unable to open file
");
}
return 0;
}
Are there are ways to simplify the program and make it more compact or less error prone?
question from:
https://stackoverflow.com/questions/66050201/how-to-clear-blank-lines-in-files-using-c 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…