Infinite loop on printing in the file - c

We were asked to store values from one file to another. (Reading it from a source txt file and copying it into another txt file) I got all the syntax correct but somehow, my program's got a logical error in printing in the second file (Infinite loop so that's why it won't proceed in the next while loop.) The product of this program is getting the last set of information entered only and the loop never ends.
Here's my program:
char ans;
FILE *fp,*fp2;
fp = fopen("INVENTORY.txt","w");
do{
printf("Date (mm/dd/yy): ");
scanf("%d %d %d", &inv.dt.m, &inv.dt.d, &inv.dt.yr);
printf("Part No.: ");
scanf("%d", &inv.pno);
printf("Price: ");
scanf("%f", &inv.price);
printf("Quantity On Hand: ");
scanf("%d", &inv.qty);
printf("Reorder Parts: ");
scanf("%d", &inv.rp);
printf("Monthly Order: ");
scanf("%d", &inv.mo);
if(inv.qty < inv.rp)
inv.oa = (inv.rp + inv.mo) - inv.qty;
else
inv.oa = 0;
fprintf(fp,"%d/%d/%d\t%d\t%.2f\t%d\t%d\t%d\t%.2f\n", inv.dt.m, inv.dt.d, inv.dt.yr, inv.pno, inv.price, inv.qty, inv.rp, inv.mo, inv.oa);
printf("Add another (Y/N)? ");
scanf(" %c",&ans);
printf("\n");
}while(ans == 'Y' || ans == 'y');
fclose(fp);
fp = fopen("INVENTORY.txt", "r");
fp2 = fopen("REPORT.txt", "w");
while(fscanf(fp,"%d/%d/%d\t%d\t%.2f\t%d\t%d\t%d\t%.2f\n", &inv.dt.m, &inv.dt.d, &inv.dt.yr, &inv.pno, &inv.price, &inv.qty, &inv.rp, &inv.mo, &inv.oa)!=EOF){
fprintf(fp2,"%d\t%.2f\t%d\t%d\t%d\t%.2f\n", inv.pno, inv.price, inv.qty, inv.rp, inv.mo, inv.oa);
}
fclose(fp);
fclose(fp2);
fp2 = fopen("REPORT.txt","r");
printf("Part No.\tPrice\tQuantity On Hand\tReorder Parts\tMonthly Order\tOrder Amount");
while(fscanf(fp2,"%d\t%.2f\t%d\t%d\t%d\t%.2f\n", &inv.pno, &inv.price, &inv.qty, &inv.rp, &inv.mo, &inv.oa)!=EOF){
printf("%d\t%.2f\t%d\t%d\t%d\t%.2f",inv.pno, inv.price, inv.qty, inv.rp, inv.mo, inv.oa);
}
fclose(fp2);
return 0;
I hope someone can help me with this, thank you.

Joven,
I think your problem lies with scanf(" %c",&ans); try removing the space infront of "%c"
The best thing for you to do would to be put some output at the end of your loops for debugging, you have a lot of loops, at the end of each loop do something like printf("\nwhile loop id: 1, seq: %d\n", loop_count) you also have some things going on with your fclose and fopens, Accesing files takes a small bit of time, it's quicker for you to open the file when you need it, and then close it when your done...If you need to read and write to a file at the same time use fopen(filename, "rw").

Related

How to add line number to only a specific line in a file

I'm creating the inputs of a disease and information on it. The disease should have a number beside it but the remaining inputs should remain as is. How can i add the information to a file but have the disease number increment as it reads new inputs as the program closes?
I've tried using a variable to print the disease number as it goes, but i dont understand how to increment it.
void CreateNew(){
int diseasenum=1;
FILE*fptr;
fptr = fopen("Lifeline Medical & Diagnostic Center.txt", "a+");
if(fptr == NULL)
{
printf("Error! There is no file to write to. Please Create a file");
exit(1);
}
fflush(stdin);
printf("Enter the name of the disease you would like to give detail of: ");
gets(Dissarray.Disease);
if()
fprintf(fptr,"%d\tDisease: %s\n\n",diseasenum,Dissarray.Disease);
printf("\n");
fflush(stdin);
Dissarray.Lethality=0;
printf("What is the Lethality of %s?\t(Answer in percentage. Sample:90 OR 12, etc)\n",Dissarray.Disease);
if (scanf("%d", &Dissarray.Lethality)!= 1)
{
printf("This is not an appropriate number. Please enter appropriately.\n");
fflush(stdin);
scanf("%d", &Dissarray.Lethality);
}
fprintf(fptr,"Lethality: %d\n",Dissarray.Lethality);
printf("\n");
fflush(stdin);
printf("How is %s acquired. (Sample: Contagious Disease, STI, Hereditary)\n",Dissarray.Disease);
gets(Dissarray.ContagionFactor);
fprintf(fptr,"Contagion factor: %s\n",Dissarray.ContagionFactor);
printf("\n");
printf("How is %s Transmitted?\t\t(Sample: Airborne, Touch, Sex, Sneezing, etc.)\n",Dissarray.Disease);
gets(Dissarray.Spread);
fprintf(fptr,"Spread: %s\n",Dissarray.Spread);
printf("\n");
fflush(stdin);
Dissarray.Fatalities=0;
printf("On a yearly basis. What is the average Fatality count brought by %s?\t\t(How many have died to this disease? Sample:100000)\n",Dissarray.Disease);
if(scanf("%d", &Dissarray.Fatalities)!= 1)
{
printf("This is not an appropriate number. Please enter appropriately.\n");
fflush(stdin);
scanf("%d", &Dissarray.Fatalities);
}
fprintf(fptr,"Fatalities: %d\n",Dissarray.Fatalities);
printf("\n");
fflush(stdin);
printf("Has %s been known to evolve under any conditions?\n",Dissarray.Disease);
printf("What is the Sensitivity?\t\t(Sample:Temperatures over 90 degrees OR None.)\n");
gets(Dissarray.Sensitivity);
fprintf(fptr,"Sensitivity: %s\n\n",Dissarray.Sensitivity);
printf("\n");
printf("This ends the entry of info into the file\n");
fclose(fptr);
}
to answer your question about line numbering:
regarding:
int diseasenum=1;
change to:
static int diseasenum=1;
Then, before exiting the function:
diseasenum++;

Block reading and writing using fread and fwrite in C

I am trying to read employee information from the user and enter it into a file using fwrite() function and later i want to print the data contents on the screen using fread() to read from file and then print it.
When i am inside the program, this process is working absolutely fine but after program is exited and i access the file where the information has been stored, i see unreadable characters but in the program they are printed as normal english characters and digits.
Here is my code:
#include<stdio.h>
struct emp{
int id;
char name[30];
double salary;
}S;
void main(){
char fname[60];
printf("Enter file name: ");
scanf("%s", fname);
FILE *fptr = fopen(fname, "a+"); // open file in append + mode, create if not found.
if(fptr == NULL){
printf("Some error occured !\n");
return;
}
int i, size;
printf("Enter the number of employees whose information is needed to be added to the file: ");
scanf("%d", &size);
// writing
for(i = 0 ; i < size ; i++){
printf("Employee %d:\n", i+1);
printf("Enter id: ");
scanf("%d", &S.id);
printf("Enter name: ");
while(getchar() != '\n'); // clear buffer
scanf("%s", S.name);
printf("Enter salary: ");
scanf("%lf", &S.salary);
fwrite(&S, sizeof(struct emp), 1, fptr);
printf("----------------------------------------\n");
}
rewind(fptr); // move pointer to first record in file
// reading
printf("File contents: \n");
printf("ID\t\tNAME\t\tSALARY\n");
while(fread(&S, sizeof(struct emp), 1, fptr) != 0){
printf("%d\t\t%s\t\t%lf\n", S.id, S.name, S.salary);
}
}
Here is the picture of what i am trying to expalin.
You are writing to the file the struct and not printing its contents separately. It works when you read it back, but when you open the file, it simply doesn't know what it is (you have int and char* and double in your struct.
If you want to visualize it on the file, you need to print each term of the struct individually, and read it back the same way, in order to see it on screen.

Structures saved every second index value in C

I'm writing a project and have stumbled upon a problem.
My part has to take the entries and save them as a structure into the file. Everything seems to work properly until I try to save them. That's my get_db_entry code:
printf("Please enter a forename: ");
fflush(stdin);
fgets(st[index].forename, STRING_SIZE, stdin);
printf("Please enter a surname: ");
fflush(stdin);
fgets(st[index].surname, STRING_SIZE, stdin);
printf("Please enter an address: ");
fflush(stdin);
fgets(st[index].address, STRING_SIZE, stdin);
printf("Please enter plate number: ");
fflush(stdin);
fgets(st[index].plate_number, STRING_SIZE, stdin);
printf("Please enter account balance: ");
fflush(stdin);
scanf("%f", &st[index].balance);
And saving looks like this:
void save_db(struct customer *st){
FILE *pFile;
int file_nof=0;
FILE *pnof;
pnof=fopen("nof_db.dat", "rb");
fread(&file_nof, sizeof(int), 1, pnof);
fclose(pnof);
printf("File_nof value: %d\n", file_nof);
fflush(stdin);
if((pFile = fopen("database.dat", "ab")) != NULL) // Open file in write/update mode
{
fwrite(st, file_nof*sizeof(struct customer), 1, pFile);
fclose(pFile);
}
else
{
printf("Cannot open file");
exit(1);
}}
I've checked numerous times and the index value is being passed on correctly, meaning that it counts 0,1,2,etc.
The entries are being saved for 0,2,4,6, so there is literally a padding by an empty structure every entry, say: index=0 always saves properly, then index=1 - empty, though the program saves into index=2 automatically.
This leaves me with empty spaces and a waste of space.
There are multiple checks along the way that check the value of "index", so I've no idea what's going on.
PS.
file_nof = nof_entries = index+1

C: writing/reading files using do while loop

I'm writing a simple exercise app in C, which should ask user if he want write something to file or read the file. I want give name to the file and then input some text in console. I want use that text to save it to the file.
The problem is that, when I already give name to the file, I can't input data to the console and by this I can't save these datas to the text file. In addition, the while loop ignores my 'y' to restart the program again. Furthermore, when I want use reading file then it really does, program works, but it also adds instruction from default (printing just error) but I don't want that, just only read from file and print it to the console.
Can someone explain me what I'm doing wrong and how to solve this? I'd be grateful.
Here's my code:
int main()
{
FILE *file;
char nameFile[32];
char string[500];
char ans[2];
int choice;
do{
printf("What do you want to do?\n");
printf("1. Write text to file\n");
printf("2. Read text file\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Give name to the file (*.txt).\n");
scanf("%s", nameFile); //This line skips me to line where program ask user if restart the program (I have no idea why :()
system("clear");
file=fopen(nameFile, "w");
if(file!=NULL){
printf("Input text:\n");
scanf("%[^\n]", string); //<--- HERE I'cant input text to console, seems like scanf doesn't work.
fprintf(file, "%s", string);
printf("\n\t\t\t-----------Ended writing.------------\n");
fclose(file);
}
else
{
printf("Could not open the file.");
return -1;
}
break;
case 2:
printf("Give name to the file (*.txt)");
scanf("%s", nameFile);
system("clear");
file=fopen(nameFile, "r");
if(file!=NULL){
while (!feof(file)) {
fscanf(file, "%s", string);
printf("%s\n",string); //After printing data from text file, adds instruction from line , and that is printing Error. How to get rid of it?
}
}
else{
printf("Could not open the file.");
return -1;
}
default:
printf("Error.");
break;
}
printf("Do you want to restart the program? (y/*)"); //Even if I write 'y', program ends anyway :(
scanf("%s", ans);
}
while(ans=='y');
return 0;
}
https://ideone.com/aCYJR5
scanf("%[^\n]", string); doesn't work unless the input buffer is clear. Use instead scanf("%499s", string) where 499 is the size of the string minus 1.
Don't use while (!feof(file)){...}, use instead while(fscanf(file, "%500s", string) == 1){...}
Use while(ans[0]=='y') as suggested earlier. Or use
char ans;
scanf(" %c", &ans);//note the blank space before `%c`
while(ans == 'y')
You also forgot to break a switch statement. Try the following:
char c;
char ans;
do {
printf("What do you want to do?\n");
printf("1. Write text to file\n");
printf("2. Read text file\n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Give name to the file (*.txt).\n");
scanf("%s", nameFile);
file = fopen(nameFile, "w");
if(file == NULL) { printf("Could not open the file."); return -1; }
printf("Input text:\n");
while((c = getchar()) != '\n' && c != EOF);
fgets(string, sizeof(string), stdin);
string[strcspn(string, "\r\n")]=0;
fprintf(file, "%s", string);
printf("\n\t\t\t-----------Ended writing.------------\n");
fclose(file);
break;
case 2:
printf("Give name to the file (*.txt)");
scanf("%s", nameFile);
file = fopen(nameFile, "r");
if(file == NULL) { printf("Could not open the file."); return -1; }
while(fgets(string, sizeof(string),file))
printf("%s\n", string);
fclose(file);
break;
}
printf("Do you want to restart the program? (y/*)");
scanf(" %c", &ans);
} while(ans == 'y');
See also
Why is “while ( !feof (file) )” always wrong?
How to clear input buffer in C?
your while should be
while(ans[0]=='n');
A char array is a string, you are trying to compare a whole string to the character 'y'. Also, it should "Loop while ans[0] is equal to 'n'" meaning keep going if the user specifies n.

C - data not being added to the txt file

I'm supposed to be creating a small client managing program for a private clinic (before you ask, yes this is college work) but I seem to have found 2 bugs that don't allow me to progress on this.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct DATA {
int dia, mes, ano;
};
struct cliente {
char pnome[15];
char snome[15];
char telefone[10];
char bi[10];
float peso, altura;
struct DATA data;
};
int main () {
FILE *c, *tempo;
int op, i;
int stelemovel, sbi;
char filename[15];
struct cliente a;
printf("1 - procurar\n");
printf("2 - inserir novo\n");
printf("3 - gravar\n");
printf("4 - sair\n");
scanf("%d", &op);
switch(op) {
**(...)**
case 2 :
printf("novo cliente. Dados: \n");
tempo = fopen("temp.txt", "a"); //opens a file to store the data on so I can copy it later, this was an attempt to correct a bug I will explain ahead
printf("primeiro nome \n");
scanf("%s", &a.pnome); //scan the first name
fprintf(tempo, "%s", a.pnome); //print to tempo file
printf("segundo nome \n");
scanf("%s", &a.snome); //second name
fprintf(tempo, "%s", a.snome);// print
printf("peso\n"); //you get the picture so far
do{
scanf("%f", &a.peso);
} while (a.peso<1);
fprintf(tempo, "%.2f", a.peso);
printf("altura\n");
do{
scanf("%f", &a.altura);
} while (a.altura<1);
fprintf(tempo, "%.2f", a.altura);
printf("por favor insira data nascimento (dia mes ano)\n");
do {
printf("dia\n");
scanf("%d", &a.data.dia);
} while (a.data.dia<1 || a.data.dia>31);
fprintf(tempo, "%d", a.data.dia);
do {
printf("mes\n");
scanf("%d", &a.data.mes);
} while (a.data.mes<1 || a.data.mes>12);
fprintf(tempo, "%d", a.data.mes);
do {
printf("ano\n");
scanf("%d", &a.data.ano);
} while (a.data.ano<1);
fprintf(tempo, "%d", a.data.ano);
printf("numero telefone\n");
do {
scanf("%s", &a.telefone);
} while (strlen(a.telefone)!=9);
fprintf(tempo, "%d", a.telefone);
printf("numero BI\n");
do {
scanf("%s", &a.bi);
} while (strlen(a.bi)!=9);
fprintf(tempo, "%d", a.bi);
/* printf("%s, %s\n %.2f, %.2f\n %d, %d, %d\n %s, %s\n", a.pnome, a.snome, a.peso, a.altura, a.data.dia, a.data.mes, a.data.ano, a.telefone, a.bi); */
/*this was something I used to test out if the data was saving properly
which is EXCEPT for the a.telefone and the a.bi, they're being printed together for some reason
*/
return main();
case 3 :
printf("nome do ficheiro\n");
scanf("%s", &filename);
c = fopen(filename, "a");
printf("%s, %s\n %.2f, %.2f\n %d, %d, %d\n %s, %s\n", a.pnome, a.snome, a.peso, a.altura, a.data.dia, a.data.mes, a.data.ano, a.telefone, a.bi);
fprintf(c, "%s, %s\n %.2f, %.2f\n %d, %d, %d\n %s, %s\n", a.pnome, a.snome, a.peso, a.altura, a.data.dia, a.data.mes, a.data.ano, a.telefone, a.bi);
/*this part basically should copy all I scanned in case 2 and print it to a document but I can't seem to be able to write on the file. The file is created opened but there's never any content on it */
return main();
**(...)**
return 0;
}
This is the bug I get when printing, a.bi gets printed normally next though which basically tells me the problem is on a.telefone but I can't seem to see it.
I'm writing this here because I'm really out of ideas, slightly saturated too and as you can probably guess I'm not exactly a pro on files. Thanks in advance for any help being provided though.
I'm sure the solution is probably simple but I can't seem to see it at this point...
EDIT
Now if ya guys can help me on why I'm not able to print the stuff I scan to a file this matter can be considered closed.
EDIT 2
Ok seems like it's printing to a file but it's not separating the words through commas.
The reason your code is printing telephone and bi together is because you didn't leave enough space for the '\0' at the end of the string. Strings in C are null terminated. This also indicates your not sanitizing your inputs. Doing scanf("%s", str); is very dangerous and leads to buffer exploits. You should provide a width to it like scanf("%8s", str); or consider using something like fgets().
The telephone string:
char telefone[9];
Is too small to held the phone number you punched in plus the null tereminator of the string. Changing it to something larger as:
char telefone[16];
Will work. But the probllem here is that you don't make any check on the input data for buffer overflow.
Always check for max input.
char telefone[9];
This buffer can hold a string of up to 8 characters, plus terminating '\0' as the 9th element. You are experiencing buffer overflow by writing XXX XXX XXX to it. Enlarge the buffer to at least 10 characters and always check the returned value of scanf in each call:
char telefone[10];
if (scanf("%s", &a.telefone) != 0)
// handle error
Consider using fgets to avoid buffer overflow:
char buf[10];
fgets(buf, sizeof(buf), stdin);
a major problem with the posted code is repeatedly calling fopen() with the same variable on a file that is already open.
Suggest calling fclose() before every new recursion AND before exiting the program.

Resources