File does not fprint correctly in C? - c

So here I have a basic program that will write to a specific line in a file by writing the contents of the file into a temporary file where the new line is written and then the contents of that file is then copied back into the starting file.
(Scores) = File
(Sub) = Temp
#include <stdio.h>
#include <conio.h>
#include <string.h>
void insert(void);
int main()
{
insert();
}
void insert(void)
{
FILE *fp,*fc;
int lineNum;
int count=0;
char ch=0;
int edited=0;
int score=0;
fp=fopen("Test 02 Scores.txt","r");
fc=fopen("Sub.txt","w");
if(fp==NULL||fc==NULL)
{
printf("\nError...cannot open/create files");
exit(1);
}
printf("Enter the score");
scanf("%d",&score);
printf("\nEnter Line Number Which You Want 2 edit: ");
scanf("%d",&lineNum);
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
count++;
if(count==lineNum-1 && edited==0)
{
if(lineNum==1)
{
fprintf(fc,"%d\n",score);
}
else
fprintf(fc,"\n%d\n",score);
edited=1;
while( (ch=fgetc(fp))!=EOF )
{
if(ch=='\n')
break;
}
}
else
fprintf(fc,"%d",ch);
}
fclose(fp);
fclose(fc);
if(edited==1)
{
printf("\nLine has been written successfully.");
char ch;
FILE *fs, *ft;
fs = fopen("Sub.txt", "r");
if( fs == NULL )
{
printf("File is not real");
exit(1);
}
ft = fopen("Test 02 Scores.txt", "w");
if( ft == NULL )
{
fclose(fs);
printf("File is not real\n");
exit(1);
}
while( ( ch = fgetc(fs) ) != EOF )
fputc(ch,ft);
printf("\nFile copied\n");
getch();
fclose(fs);
fclose(ft);
}
else
printf("\nLine Not Found");
}
However, a problem has arisen, I started to write this code for use with strings, but since decided to use number values, whenever I try to copy with the integer values the program will not copy anything right, I Know this may be caused by the char to int but I'd rather have more help in assessing where I should change stuff.

The error is in this line
fprintf(fc,"%d",ch)
%d prints ch as an integer, not as a character, you should instead write
fprintf(fc,"%c",ch)
or use fputc()
There are some small issues with your code, here is a working version. I added comments where I changed things.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h> // needed for exit()
void insert(void);
int main()
{
insert();
}
// use fgets to read from keyboard, it is simpler.
int readNumber()
{
char buffer[64] = {0};
fgets(buffer, sizeof(buffer), stdin);
return atoi(buffer);
}
void insert(void)
{
FILE *fp = NULL; // prefer one decl per row
FILE *fc = NULL;
int lineNum = 0;
int count=0;
int ch=0; // should be int ch=0;
int edited=0;
int score=0;
// file names
const char src[] = "Test 02 Scores.txt";
const char dest[] = "Sub.txt";
fp=fopen(src,"r");
if(fp==NULL)
{
perror(src); // use perror() instead for better error msg
exit(EXIT_FAILURE); // there are std constants for exit args
}
fc=fopen(dest,"w");
if(fc==NULL)
{
perror(dest);
exit(EXIT_FAILURE);
}
printf("Enter the score: ");
score = readNumber(); // using fgets to avoid lingering \n in buffer
printf("\nEnter Line Number Which You Want 2 edit: ");
lineNum = readNumber();
while((ch=fgetc(fp))!=EOF) // fgetc returns int so ch should be int
{
if(ch=='\n') // better to have {} here too
{
count++;
}
if(count==lineNum-1 && edited==0)
{
if(lineNum==1)
{
fprintf(fc,"%d\n",score);
}
else // better to { } here too
{
fprintf(fc,"\n%d\n",score);
}
edited=1;
// i guess you want to remove old score
while( (ch=fgetc(fp))!=EOF )
{
if(ch=='\n')
{
break;
}
}
}
else // {} for avoiding future pitfall
{
fputc(ch,fc);
}
}
fclose(fp);
fclose(fc);
if(edited==1)
{
puts("\nLine has been written successfully."); // puts() when u can
int ch = 0; // int
FILE *fs = NULL;
FILE *ft = NULL;
fs = fopen(dest, "r");
if( fs == NULL )
{
perror(dest);
exit(EXIT_FAILURE);
}
ft = fopen(src, "w");
if( ft == NULL )
{
perror(src);
exit(EXIT_FAILURE); // at program exit files will close anyway
}
while( ( ch = fgetc(fs) ) != EOF )
{
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
printf("\nFile copied\n");
getch();
}
else
{
printf("\nLine Not Found");
}
}

Related

C program to print line number in which given string exists in a text file

I have written a C program that opens a text file and compares the given string with the string present in the file. I'm trying to print the line number in which the same string occurs, but I am unable to get the proper output: output does not print the correct line number.
I would appreciate any help anyone can offer, Thank you!
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
int num = 0, line_number = 1;
char string[50];
char student[100] = { 0 }, chr;
while (student[0] != '0') {
FILE *in_file = fopen("student.txt", "r");
if (in_file == NULL) {
printf("Error file missing\n");
exit(1);
}
printf("please enter a word \n");
scanf("%s", student);
while (fscanf(in_file, "%s", string) == 1) {
if (chr == '\n') {
if (strstr(string, student) == 0) {
break;
} else
line_number += 1;
}
}
printf("line number is: %d\n", line_number);
fclose(in_file);
}
return 0;
}
You cannot read lines with while (fscanf(in_file, "%s", string), the newlines will be consumed by fscanf() preventing you from counting them.
Here is an alternative using fgets():
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char string[200];
char student[100];
int num = 0, line_number = 1;
FILE *in_file = fopen("student.txt", "r");
if (in_file == NULL) {
printf("Error file missing\n");
exit(1);
}
printf("please enter a word \n");
if (scanf("%s", student) != 1) {
printf("No input\n");
exit(1);
}
while (fgets(string, sizeof string, in_file)) {
if (strstr(string, student)) {
printf("line number is: %d\n", line_number);
}
if (strchr(string, '\n')) {
line_number += 1;
}
fclose(in_file);
}
return 0;
}

How to give access to text file using if statement?

I am trying to give an if statement to check if a particular name is present in text file, then give access to it otherwise give error message.
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("For person details, please enter the person name: \n");
FILE * fr = fopen("/home/bilal/Documents/file.txt","r");
int catch, i=0, index=0;
char ch[100];
printf("Enter your Name: ");
if (scanf("%s", )){ // Don't know what to put here?
perror("Error while reading!");
return 0;
}
catch = fgetc(fr);
while(catch != EOF){
ch[index] = catch;
if (ch[index] == ' '){
ch[index] = '\0';
printf("Here is your result: %s\n",ch);
index = 0;
i++;
}
else
index++;
catch = fgetc(fr);
}
fclose(fr);
return 0;
}
Simply the program firstly opens a file and asks for a user input and verifies if the provided content is case-sensitively matched with the file. If so, then it'll let the program access the entire file and display on the screen, to do that, we must use another FILE b/c the old *fp is already manipulated and in case it's reused, it may display wrong data.
#include <stdio.h>
#include <string.h>
int main(void) {
FILE *fp = fopen("file.txt", "r"); // for verification
FILE *fp1 = fopen("file.txt", "r"); // for future use
char ch[50], str[50];
short int FLAG = 0;
printf("Enter the string: ");
scanf("%s", &str); // asks for input
while (fscanf(fp, "%s", ch) != EOF) {
if (!strcmp(ch, str)) { // checks if a string matches provided by the user
printf("Found! Here's your details...\n\n");
FLAG = 1;
}
}
if (!FLAG == 1) { // no? exits.
printf("Not found, access denied!\n");
return -1;
}
fclose(fp);
int c = fgetc(fp1); // yes? let's go...
while (c != EOF) {
printf("%c", c); // displays containing data
c = fgetc(fp1);
}
fclose(fp1);
return 0;
}
You'll want to add a variable for your scanf output:
char name[100];
if (scanf("%s", name) != -1)
// ...
Then to compare both you'll use strcmp.
#include <string.h>
//...
if (strcmp(ch, name) == 0)
// both are equal
Note that you can access documentation for scanf and strcmp by typing man scanf or man strcmp in your terminal.
int main()
{
printf("For person details, please enter the person name and id card
number: \n");
printf("Enter your Name: ");
char personName[100];
scanf("%s", personName);
printf("Enter your card number: ");
int cardNumber;
if (scanf("%d", &cardNumber)){
printf("no error detected");
}
else{
printf("error while reading");
}
return 0;
}
The fixed code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
printf("For person details, please enter the person name: \n");
FILE* fr = fopen("/home/bilal/Documents/file.txt", "r");
int catch, i = 0, index = 0;
char ch[100] = { 0 };
if (fr == NULL)
{
perror("Invalid file opening!");
return 1;
}
printf("Enter your Name: ");
fgets(ch, 100, fr);
size_t len = strcspn(ch, "\n");
ch[(len < 100) ? (len) : (99)] = 0; // For file safety checking
if (strlen(ch)) { // Don't know what to put here?
perror("Error while reading!");
return 1;
}
catch = fgetc(fr);
while (catch != EOF) {
ch[index] = catch;
if (ch[index] == ' ') {
ch[index] = '\0';
printf("Here is your result: %s\n", ch);
index = 0;
memset(ch, 0, 100);
i++;
}
else
{
index++;
}
catch = fgetc(fr);
}
fclose(fr);
return 0;
}

My delete function deletes the wrong record of struct

I have this function which is supposed to delete the recipe requested by the user. I've always deleted records in this way, but I don't know why this time it doesn't work in the right way. Sometimes it states that there's no match but it deletes the recipe or it deletes all the recipes inside the file and I don't get it why since I basically created a temp file in which writing all the records except for the one the user wants to delete and I used a function that worked for another record so I really don't get it. Is that because the length of ingredients and procedure is not the same for all the recipes?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int stringCmpi (char *s1,char *s2);
struct _recipe
{
char name[40];
int count_i;
char ingredients[20][40];
char diff[12];
int timr;
int calories;
int count_p;
char procedure[30][500];
} recipe;
int main()
{
FILE* fbr;
FILE* temp;
char ricetta_name[] = "ricette.bin";
char temp_name[] = "temp.bin";
fbr = fopen("ricette.bin", "rb");
temp = fopen("temp.bin", "wb");
int found = 0;
char name_t[40];
int i;
char space = '\n';
if(fbr == NULL)
{
printf("Couldn't open the file.\n");
exit(1);
}
if(fbr == NULL)
{
printf("Couldn't open the file.\n");
exit(1);
}
printf("Write the name of the recipe you want to delete:\n");
fgets(name_t,sizeof(name_t),stdin);
space = getchar();
while(fread(&recipe,sizeof(recipe),1,fbr) && found == 0)
{
if(stringCmpi(name_t,recipe.name) == 0)
{
found = 1;
printf("Match found. Recipe deleted.\n");
}
else
{
fwrite(&recipe,sizeof(recipe),1,temp);
}
if(!found)
{
printf("No match.\n");
}
}
fclose(fbr);
fclose(temp);
remove(ricetta_name);
rename(temp_name,ricetta_name);
system("PAUSE");
return 0;
}
int stringCmpi (char *s1,char *s2)
{
int i=0;
for(i=0; s1[i]!='\0'; i++)
{
if( toupper(s1[i])!=toupper(s2[i]) )
return 1;
}
return 0;
}
The problem is here:
while(fread(&recipe,sizeof(recipe),1,fbr) && found == 0)
Once you find a recipe, you exit the loop, so the remaining elements are not copied to the new file. I recommend you delete the && found == 0 clause.
Furthermore:
if(!found)
{
printf("No match.\n");
}
This is actually inside the while loop, so it will report No match multiple times. Please move it outside the loop.
Proper indentation would help you see the actual structure of the program. This is how the posted program looks with consistent indentation:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int stringCmpi (char *s1,char *s2);
struct _recipe
{
char name[40];
int count_i;
char ingredients[20][40];
char diff[12];
int timr;
int calories;
int count_p;
char procedure[30][500];
} recipe;
int main()
{
FILE* fbr;
FILE* temp;
char ricetta_name[] = "ricette.bin";
char temp_name[] = "temp.bin";
fbr = fopen("ricette.bin", "rb");
temp = fopen("temp.bin", "wb");
int found = 0;
char name_t[40];
int i;
char space = '\n';
if(fbr == NULL)
{
printf("Couldn't open the file.\n");
exit(1);
}
if(fbr == NULL)
{
printf("Couldn't open the file.\n");
exit(1);
}
printf("Write the name of the recipe you want to delete:\n");
fgets(name_t,sizeof(name_t),stdin);
space = getchar();
while(fread(&recipe,sizeof(recipe),1,fbr) && found == 0)
{
if(stringCmpi(name_t,recipe.name) == 0)
{
found = 1;
printf("Match found. Recipe deleted.\n");
}
else
{
fwrite(&recipe,sizeof(recipe),1,temp);
}
if(!found)
{
printf("No match.\n");
}
}
fclose(fbr);
fclose(temp);
remove(ricetta_name);
rename(temp_name,ricetta_name);
system("PAUSE");
return 0;
}
int stringCmpi (char *s1,char *s2)
{
int i=0;
for(i=0; s1[i]!='\0'; i++)
{
if( toupper(s1[i])!=toupper(s2[i]) )
return 1;
}
return 0;
}
The program is checking that fbr is not NULL twice. Probably the second check should be for temp.
The programs stops reading and copying when the recipe to be deleted is found; this means that the following records are not copied over.
The program complains that the recipe was not found for each recipe until the desired one is found.
Suggestions:
Modify the while loop condition eliminating the && ! found, so that all records are copied except those matching the given name.
Move the check for if (! found) outside the loop.
Bonus:
The function stringCmpi() stops comparing at the end of string s1; which means that "abc" and "abcdef" will compare equal. You probably want to change return 0 to return s1 [i] != s2 [i].
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _recipe
{
char name[40];
int count_i;
char ingredients[20][40];
char diff[12];
int timr;
int calories;
int count_p;
char procedure[30][500];
} recipe;
int main(int argc, char *argv[])
{
FILE* fbr;
FILE* temp;
char *ricetta_name = "recipe.bin";
char *temp_name = "temp.bin";
int found = 0;
char name_t[40];
// Use command line arguments if possible
if (argc == 2)
{
ricetta_name = argv[1];
}
if((fbr = fopen(ricetta_name, "rb")) == NULL)
{
printf("Couldn't open the file %s.\n", ricetta_name);
exit(1);
}
if((temp = fopen(temp_name, "wb")) == NULL)
{
printf("Couldn't open the file %s.\n", temp_name);
exit(1);
}
printf("Write the name of the recipe you want to delete:\n");
fgets(name_t, sizeof(name_t), stdin);
// Remove '\n'
if (name_t[strlen(name_t) - 1] == '\n')
name_t[strlen(name_t) - 1] = 0;
while(fread(&recipe,sizeof(recipe), 1, fbr))
{
// Use standard strcasecmp()
if(strcasecmp(name_t, recipe.name) == 0)
{
found = 1;
printf("Match found. Recipe deleted.\n");
}
else
{
fwrite(&recipe, sizeof(recipe), 1, temp);
}
}
if (!found)
{
printf("No match.\n");
}
fclose(fbr);
fclose(temp);
remove(ricetta_name);
rename(temp_name,ricetta_name);
return 0;
}
Please format your code: add space(' ') after comma(','). And use standard function if possible.

File Handling in C(using fread & fwrite)

doing this simple program. This is just the firt part of a banking service program. Somehow, I am stuck with this fread() . I cant compare the input of the user and my database. When my program starts, after I input user and pass it kinda 'hang' or 'freeze' then a popup will appear and says "end program". BTW I am using Dev C++.
#include<stdio.h>
#include<string.h>
struct client
{
char accnum[9];
char accode[5];
char fname[20];
char lname[20];
}s;
main()
{
FILE *fp;
char user[9];
char pass[5];
fp=fopen("account.txt","a");
if(fp!=NULL)
{
/**
strcpy(s.accnum,"abcd1234");
strcpy(s.accode,"1234");
strcpy(s.fname,"john");
strcpy(s.lname,"doe");
fwrite(&s,sizeof(s),1,fp);
**/
printf("BANKING SERVICE");
printf("\nInput User: ");
gets(user);
printf("\nInput Pass: ");
gets(pass);
while(!feof(fp))
{
while(fread(&s,sizeof(s),1,fp)==1);
{
if(ferror(fp))
{
printf("error");
}
if (strcmp(user,s.accnum) == 0 && strcmp(pass,s.accode) == 0)
{
printf("\n\nsuccess!");
}
else
{
printf("\n\nerror!");
}
}
}
fclose(fp);
}
fclose(fp);
getch();
}
In the loop check for successful match. If no match is found, the found flag will still be zero. Then report the problem after the while loop has completed.
fgets will include the newline so it has to be removed before comparing to the contents of the file as the commented section does not show any newlines.
#include<stdio.h>
#include<string.h>
struct client
{
char accnum[9];
char accode[5];
char fname[20];
char lname[20];
}s;
int main()
{
FILE *fp;
int found = 0;
char user[20];//allow extra
char pass[20];
fp=fopen("account.txt","a");
if(fp==NULL)
{
perror ("could not open file ");
return 1;
}
/**
strcpy(s.accnum,"abcd1234");
strcpy(s.accode,"1234");
strcpy(s.fname,"john");
strcpy(s.lname,"doe");
fwrite(&s,sizeof(s),1,fp);
**/
printf("BANKING SERVICE");
printf("\nInput User: ");
fgets(user, sizeof ( user), stdin);
if ( user[strlen(user)-1] == '\n') {
user[strlen(user)-1] = '\0';//remove newline
}
printf("\nInput Pass: ");
fgets(pass, sizeof ( pass), stdin);
if ( pass[strlen(pass)-1] == '\n') {
pass[strlen(pass)-1] = '\0';//remove newline
}
while(fread(&s,sizeof(s),1,fp)==1)
{
if (strcmp(user,s.accnum) == 0 && strcmp(pass,s.accode) == 0)
{
printf("\n\nsuccess!");
found = 1;
break;
}
}
if ( found == 0)
{// after entire file has been read, report problem
printf("\n\nno match for that account and passcode!");
}
fclose(fp);
getchar();
return 0;
}
You need to check for end of file.
while( !feof( fp ) )
{
fread(&s,sizeof(s),1,fp);
if( ferror( fp ) )
{
// Error occurred
break;
}
if (strcmp(user,s.accnum) == 0 && strcmp(pass,s.accode) == 0)
{
printf("\n\nsuccess!");
}
else
{
printf("\n\nerror!");
}
}

Store and eliminating garbage values

Everytime I try to run the code it'll print out the contents of the file, however it will print out a garbage value at the end which I don't know how to get rid of. I am supposed to to store the contents of the file into an array, however I am a bit confused on how to do that???
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char filePrinter(char*arr)
int main (int argc, char**argv)
{
char fileArray[150];
if(argc !=2)
{
printf("Invalid Entry. Please Enter name of program followed by input filename\n");
}
filePrinter(fileArray);
return 0;
}
char filePrinter(char*arr)
{
int i;
FILE*file;
i=0;
file=fopen("assests/room.txt","r");
if(file == NULL)
{
printf("Could not open file\n");
exit(-1);
}
else
{
while(0 ==feof(file))
{
i=fgetc(file);
printf("%c", i);
}
}
fclose(file);
return i;
}
file content:
10x16 ds5 h6,5 g7,8 p3,3
10X16 de4 h5,7 g9,2
10X16 dw6,h2,3 m6,7
10X16 dn3,h2,4 p2,3
10X16 de2 h9,9 m4,5
10X16 dn8 h4,5 g1,1*/
feof returns true if the last call to a read operation hit EOF. You'd want to test it after the fgetc call. Or, even better, just check whether fgetc returned the special value EOF.
(A FILE * has an "end-of-file marker" that says whether some read operation has hit EOF. Read operations set the "end-of-file marker" upon hitting EOF. Before you've hit---meaning tried to read past---the end of the file, that "end-of-file marker" is clear.)
Timing is bad than look at the beginning of the loop by feof because EOF occur in fgetc.
replace to
while(EOF!=(i=fgetc(file))){
printf("%c", i);
}
int filePrinter(char*arr){
int i = 0, ch;
FILE*file;
file=fopen("assests/room.txt","r");
if(file == NULL) {
printf("Could not open file\n");
exit(-1);
} else {
while(EOF!=(ch=fgetc(file))) {
//printf("%c", ch);
arr[i] = ch; //*arr++ = ch;
++i;//i : range check
}
arr[i] = '\0';
}
fclose(file);
return i;
}
I think the code should be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void filePrinter(char*arr);
int main (int argc, char**argv)
{
char fileArray[150];
memset(fileArray, 0, sizeof(fileArray));
if(argc !=2)
{
printf("Invalid Entry. Please Enter name of program followed by input filename\n");
}
filePrinter(fileArray);
return 0;
}
void filePrinter(char *arr)
{
int c = 0, j = 0;
FILE* file = NULL;
file=fopen("assests/room.txt","r");
if(file == NULL)
{
printf("Could not open file\n");
exit(-1);
}
else
{
while (1)
{
c = fgetc(file);
if (c != EOF)
{
arr[j++] = c;
}
else
{
break;
}
}
}
fclose(file);
return;
}

Resources