I'm trying to compare 2 text files and print the first line where they differ but I'm using a buffer of 500 in the fgets() command and I think I'm wasting space.
How can I make the same program if I don't know the length of the line?
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
FILE *fp1, *fp2;
int nLine = 1;
char l1[500], l2[500];
system("clear");
if (argc < 3)
{
printf("Usage: %s <file1.txt> <file2.txt>\n",argv[0]);
exit(1);
}
if ((fp1 = fopen(argv[1],"r")) == NULL){
printf("Can't open file: %s\n", argv[1]);
exit(1);
}
if ((fp2 = fopen(argv[2],"r")) == NULL){
printf("Can't open file: %s\n", argv[2]);
exit(1);
}
fgets(l1,500,fp1);
fgets(l2,500,fp2);
while ((l1 != 0) && (l2 != 0)){
if(strcmp(l1,l2) != 0){
printf("Line number: %d\n", nLine);
printf("%s", l1);
printf("%s\n", l2);
exit(1);
} else {
fgets(l1,500,fp1);
fgets(l2,500,fp2);
nLine++;
}
}
return 0;
}
If you do not want to "waste space", remember that the data are in file memory. Read 1 character at time. When you find a difference, just seek to that location of the previous line feed and report the following lines.
long index = 0;
long index_lf = 0;
int c1,c2;
// read until a difference or done
while ((c1 = fgetc(fp1)) == (c2 = fgetc(fp2)) && (c1 != EOF)) {
index++;
if (c1 == '\n') index_lf = index;
}
if (c1 == c2) {
puts("same");
} else {
puts("differ");
fseek(fp1, index_lf, SEEK_SET);
fseek(fp2, index_lf, SEEK_SET);
// read and print each file until a following \n or EOF occurs.
// TBD code for OP
}
[Edit] Some improvements to cope with various issues: mis-match on last byte, files opened in different modes, error handling, etc.
long offset1 = ftell(fp1);;
long offset2 = ftell(fp2);;
int c1,c2;
// read until a difference or done
while ((c1 = fgetc(fp1)) == (c2 = fgetc(fp2)) && (c1 != EOF)) {
if (c1 == '\n') {
offset1 = ftell(fp1);
offset2 = ftell(fp2);
}
}
if (offset1 == -1 || offset2 == -1 || ferror(fp1) || ferror(fp2)) {
puts("problem");
} else if (c1 == c2) {
puts("same");
} else {
puts("differ");
fseek(fp1, offset1, SEEK_SET);
fseek(fp2, offset2, SEEK_SET);
// read and print each file until a following \n or EOF occurs.
// TBD code for OP
}
Related
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 != '\n') // Checking for blank lines
{
isEmpty = 0;
}
else
{
if(c == '\n' && isEmpty == 0) // Read a word; Print to tmp file
{
buffer[i] = '\0';
fprintf(tmp, "%s", buffer);
i = 0;
isEmpty = 1;
}
else if(c == '\n' && isEmpty == 1) // NOT SURE WHY THIS IS IMPORTANT
{
buffer[i] = '\0';
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\n");
}
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 != '\n' && c != ' ' && c != '\0' && c != ',')
{
isEmpty = 0;
buffer[i++] = c;
}
else
{
if( c == '\n' && isEmpty == 0 )
{
buffer[i] = '\0';
fprintf(tmp, "%s", buffer);
i = 0;
isEmpty = 1;
}
else if( c == '\n' && isEmpty == 1 )
{
buffer[i] = '\0';
i = 0;
}
}
}
fclose(tmp);
}
else
{
printf("Unable to open temporary file\n");
}
fclose(file);
}
else
{
printf("Unable to open file\n");
}
return 0;
}
Are there are ways to simplify the program and make it more compact or less error prone?
Only problem is that when one file is at EOF, program still writes - or +, just need to make some condition to make it just takes words from one file when other is at EOF. For example
prvy.txt: Ahojte nasi studenti ktori maju radi programovanie
druhy.txt: vsetci mili
treti.txt:
+Ahojte -vsetci +nasi -mili +studenti +ktori +maju +radi +programovanie
#include<stdio.h>
#include<stdlib.h>
int main(){
FILE *first, *second, *third;
char ch[256],ch1[256];
int i=1,count=0, ch2;
char space = ' ';
char minus = '-';
char plus = '+';
first=fopen("prvy.txt", "r");
second=fopen("druhy.txt", "r");
third=fopen("treti.txt", "w");
if(first==NULL || second==NULL || third==NULL)
{
perror("error");
exit(1);
}
while (fscanf(first, "%255s", ch) == 1)
{
count++;
}
while (fscanf(second, "%255s", ch) == 1)
{
count++;
}
printf("%d",count);
rewind(first);
rewind(second);
for(i;i<=count;i++)
{
if(i%2==1)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF && ch2 != ' ' && ch2 != '\n') {
putc(ch2,third);
ch2=fgetc(first);
}
}
else if(i%2==0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF && ch2 != ' ' && ch2 != '\n') {
putc(ch2,third);
ch2=fgetc(second);
}
}
putc(space,third);
}
fclose(first);
fclose(second);
fclose(third);
return 0;
}
Your code will alternate between the two files. That will not work as the files may contain different number of words.
One solution could be to count the words in one variable per file. Then the loop could be something like:
// count1: number of words in first file
// count2: number of words in second file
while(count1 > 0 || count2 > 0)
{
if (count1 > 0)
{
fputc(plus,third);
ch2=fgetc(first);
while(ch2 != EOF && ch2 != ' ' && ch2 != '\n') {
putc(ch2,third);
ch2=fgetc(first);
}
--count1;
}
if (count2 > 0)
{
fputc(minus,third);
ch2=fgetc(second);
while(ch2 != EOF && ch2 != ' ' && ch2 != '\n') {
putc(ch2,third);
ch2=fgetc(second);
}
--count2;
}
putc(space,third);
}
You don't need to scan both files first to get a count. Instead, create an array of two input files and use an index to toggle between both as you read. When a file is exhausted when its turn has come, scan and print the other one.
That way, you get rid of the need to control the succesful input of two files simultaneously:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *in[2]; // Two alternating input files
FILE *out;
char line[80];
char prefix[] = "+-"; // Alternating signs, +/-
int index = 0; // index to in[] and prefix[]
in[0] = fopen("1.txt", "r");
in[1] = fopen("2.txt", "r");
out = fopen("3.txt", "w");
if (!(in[0] && in[1] && out)) {
perror("fopen");
exit(1);
}
while (fscanf(in[index], "%79s", line) == 1) {
fprintf(out, "%c%s ", prefix[index], line);
index = !index;
}
while (fscanf(in[!index], "%79s", line) == 1) {
fprintf(out, "%c%s ", prefix[!index], line);
}
fclose(in[0]);
fclose(in[1]);
fclose(out);
return 0;
}
I'm working on Shift cipher, I am having problems with encryption. It has no errors or trouble compiling but after I run it the output file is empty. i think reading the file but not encrypted out.txt file is empty. i didn't solve it. Thank you.
int main
{
file_in = fopen("/Users/mathmoiselle/Desktop/lucky.txt", "r");
if( file_in == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
file_out = fopen("/Users/mathmoiselle/Desktop/out.txt","r");
return 0;
}
Following on from my comments. You need to rewind the file pointer for file_in and also your includes were poorly formatted at the top. Not sure whether this makes a difference (beginner myself, but certainly stuck out when I read it):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int encode (int, int);
int encode(int ch, int key) {
if (islower(ch)) {
ch = (ch-'a' + key) % 26 + 'a';
ch += (ch < 'a') ? 26 : 0;
}
else if (isupper(ch)) {
ch = (ch-'A' + key) % 26 + 'A';
ch += (ch < 'A') ? 26 : 0;
}
return ch;
}
int main (void)
{
FILE *file_in;
FILE *file_out;
char ch;
char text[300];
int key;
// gets(text); // Removed in question
file_in = fopen("shift_cipher.c", "r");
if( file_in == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("\n The contents of the file are : \n");
while( ( ch = fgetc(file_in) ) != EOF )
{
printf("%c",ch);
}
rewind(file_in);
// while (fgets(line, MAXLINE, f1)) {
// printf("%s", line);
// }
// gets(text); // Removed in question
file_out = fopen("out.txt","w");
printf("\n Enter the alphabetic offset key you would like to use:");
scanf("%d", &key);
while( ( ch = fgetc(file_in) ) != EOF )
{
printf("%c", ch);
ch=encode(ch, key);
fprintf(file_out, "%c", ch);
}
printf("file has been encoded");
fclose(file_out);
fclose(file_in);
return 0;
}
I am trying to test if the character in a file.txt is a space ' ' or not using this code:
char *Appartient (FILE *f, char *S)
{
int i = 0, nbdechar = 0, nbocc = 0, PosdePremierChar, space = 0;
char c;
while ((c = getc(f)) != EOF) {
PosdePremierChar = ftell(f);
if (c == S[0]) {
nbdechar = 0;
for (i = 1; i < strlen(S); i++) {
c = getc(f);
if (c == S[i]) {
nbdechar++;
}
}
if (nbdechar == strlen(S) - 1) {
nbocc++;
} else {
rewind(f);
fseek(f, PosdePremierChar - 1, SEEK_CUR);
while ((c = getc(f)) != ' ');
}
} else {
while ((c = getc(f)) != ' ') {
space++;
}
}
}
printf("\n Le nb d'occurence est %d", nbocc);
if (nbocc == 0) {
return "false";
} else {
return "true";
}
}
but a weird symbol 'ے' appear like a garbage when I inspect the variable 'c' in my debugger:
What is wrong
Could be the result of converting the end-of-file result from getc(), EOF, (which is standardized to be negative, often -1) to a character.
Note that your loop never terminates if there's no space in the file, since EOF != ' ' and that condition keeps being true after you hit end-of-file for the first time.
Modify your code like this, trace it and you might become enlightened regarding the relation between what getc() returns and how this correlates to chars:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int result = EXIT_SUCCESS;
FILE * f = fopen("test.txt", "r");
if (NULL == f)
{
perror("fopen() failed");
result = EXIT_FAILURE;
}
else
{
int result = EOF;
while (EOF != (result = getc(f)))
{
char c = result;
printf("\n%d is 0x%02x is '%c'", result, result, c);
if (' ' == c)
{
printf(" is space ");
}
}
printf("\nread EOF = %d = 0x%x\n", result, result);
fclose(f);
}
return result;
}
You didn't test if f opened, in case it didn't then undefined behavior will happen, check if the file opened
FILE *file;
int chr;
if ((file = fopen("test.txt", "r")) == NULL)
{
fprintf(stderr, "Cannot open `test.txt'\n");
return -1;
}
while (((chr = fgetc(file)) != EOF) && (chr == ' '))
printf("space\n");
You should declare chr of type int, because fgetc() returns an int, as for example EOF requires to be an int and not a char.
Also, debug mode is useful for tracking the values of variables, I bet that it can five you the value in ascii or decimal or hex, as you need if you know how to ask.
I am trying to make word search using fgetc. I understand what fgetc does but i am getting seg fault. on running the gdb test, i returns the following. Is there an easier way to implement the search function?? i am new to programming.
thank you for the help.
#0 0x00007ffff7aa4c64 in getc () from /lib64/libc.so.6
#1 0x000000000040070c in main ()
Where am i going wrong?
#include <stdio.h>
#include <stdlib.h>
int isAlpha(char c)
{
if( c >= 'A' && c <='Z' || c >= 'a' && c <='z' || c >= '0' && c <= '9' )
{
return 1;
}
else
{
return 0;
}
}
int CheckFunctionn(int length, int message_counter, char ref_word[], char newmessage[])
{
int newCounter = 0;
int counterSuccess = 0;
while(newCounter < length)
{
if(ref_word[newCounter] == newmessage[newCounter + message_counter])
{
counterSuccess++;
}
newCounter++;
}
if(counterSuccess == length)
{
return 1;
}
else
{
return 0;
}
}
int main(int argc, char *argv[])
{
char message[300];
int counter = 0;
int ref_length = 0;
int alphaCounter = 0;
int alphaCounterTime = 0;
int messageCounter = 0;
int word_counter = 0;
FILE* input;
FILE* output;
//long fileLength;
//int bufferLength;
//char readFile;
//int forkValue;
input = fopen(argv[2],"r");
output = fopen(argv[3],"w");
int c;
c = fgetc(input);
while(c != EOF)
{
while((argv[1])[ref_length] !='\0')
{
// if string is "HEY", (argv[1]) is HEY, ref_counter is the length
// which in this case will be 3.
ref_length++; //<-- takes care of the length.
}
while(alphaCounter < ref_length)
{
// this will add to alphaCounter everyetime alphaCT is success.
alphaCounterTime += isAlpha((argv[1])[alphaCounter]);
alphaCounter++;
}
if(alphaCounterTime != ref_length)
{
return 0;
}
if((messageCounter == 0 ) && (message[messageCounter + ref_length] == ' ' || message[messageCounter] == '\n' || message[messageCounter]== '\t')) // counts the whole things and brings me to space
{
// compare the message with the word
word_counter += CheckFunctionn(ref_length, messageCounter, argv[1], message);
}
if((message[messageCounter] == ' ' || message[messageCounter] == '\n' || message[messageCounter]== '\t') && (message[messageCounter + ref_length + 1] == ' ' || message[messageCounter + ref_length + 1] == '\n' || message[messageCounter + ref_length + 1]== '\t'))
{
word_counter += CheckFunctionn(ref_length, messageCounter + 1, argv[1], message);
}
if((message[messageCounter]== ' '|| message[messageCounter] == '\n' || message[messageCounter]== '\t') && (messageCounter + ref_length+1)== counter) //<-- this means the length of the message is same
{
word_counter += CheckFunctionn(ref_length, messageCounter + 1, argv[1], message);
}
messageCounter++;
}
fclose(input);
fclose(output);
return 0;
}
You're almost certainly failing to open the input file. If fopen fails, it returns NULL, and calling fgetc(NULL) has undefined behavior, and a segmentation fault is one possible outcome of undefined behavior.
You need to check for errors and handle then accordingly. You also need to check if your program was given sufficient arguments. Here's one way to handle them:
if (argc < 3)
{
fprintf(stderr, "Usage: %s input-file output-file\n", argv[0]);
exit(1);
}
input = fopen(argv[1],"r");
if (input == NULL)
{
fprintf(stderr, "Error opening input file %s: %s\n", argv[1], strerror(errno));
exit(1);
}
output = fopen(argv[2],"w");
if (output == NULL)
{
fprintf(stderr, "Error opening output file %s: %s\n", argv[2], strerror(errno));
exit(1);
}
You only read one character into c, then loop while(c != EOF) which is almost always an infinite loop. Inside that loop, you increment messageCounter which you use to walk past the end of an array -- boom!
Per your comment, argc is 2, but you refer to argv[2] which would be the third element of the args, and will be NULL. The FILE * is going to end up being NULL too (because it's invalid to pass NULL to fopen).
It will be very easy if you use strcmp function in this...
What you have to do is first find the length of ur file using ftell and after that allocate that much memory then fill that memory using fgetc or fgets or any other file function...then just use strcmp function on that....bingo!!!!! :)