Modifying values of existing text file (C Programming Language) - c

Let's say I have a file name already created named "room.txt" and it contains the following:
20 10 5
Once I execute the code below, I will be asked to specify which variable to decrease its value by 1.For example: If I input "1" it will reduce the number "20" in "room.txt".However, it doesn't reduce the value.Instead, it deletes the contents of "room.txt" file.How do I make it so that it retains the values in the file when I enter my input from the program?Also, how do I reduce the values '20' '10' '5' respectively when I input '1' '2' '3' in my program?
#include<stdio.h>
char rooms[]={"rooms.txt"};
struct rooms {
int stdsuite;
int premsuite;
int deluxesuite;
};
int stdsuite;
int premsuite;
int deluxesuite;
void availablerooms ()
{
FILE*fp;
fp = fopen(rooms,"r");
fscanf(fp,"%d %d %d",&stdsuite,&premsuite,&deluxesuite);
system("cls");
system("cls");
printf("\n\n");
printf("\t\t-----------------------------------------------\n");
printf("\t\t Room Availability\n");
printf("\t\t-----------------------------------------------\n\n");
printf("--------------------------------------------------------------------------------\n\n");
printf("\t\t Standard Suite :\t%d / 20\n\n",stdsuite);
printf("\t\t Premium Suite :\t%d / 10\n\n",premsuite);
printf("\t\t Deluxe Suite :\t%d / 5\n\n",deluxesuite);
printf("--------------------------------------------------------------------------------\n\n");
fflush(stdin);
}
int main ()
{
int choice;
FILE*fp;
availablerooms();
printf("1. Standard Suite\n\n2. Premium Suite\n\n3. Deluxe Suite\n\n");
printf("Please enter the selected room value (1-3): ");
scanf("%d",&choice);
fflush(stdin);
if (choice == 1)
{
fp = fopen(rooms,"w");
stdsuite--;
}
else if (choice == 2)
{
fp = fopen(rooms,"w");
premsuite--;
}
else if (choice == 3)
{
fp = fopen(rooms,"w");
deluxesuite--;
}
else
printf("\nThe input is invalid!");
}
Any help would be greatly appreciated! :)

To accomplish what you're trying to do in standard C you must:
Open the file for reading "r".
Read its contents to your own datastructure.
Modify the data structure.
freopen the file for writing "w".†
Write back the modified data structure.
Close the file.
† This is necessary because there is no standard C way to truncate the file in "r+" mode.

availablerooms should fclose(fp) before returning. And fflush(stdout) instead of stdin.
Then, when trying to write the data back, you only fopen the file, but you don't write anything to it. There you have a major misconception of how the things work. in the 3 ifs, you decrement the relevant variables. But that are purely in-memory operations. opening the file before, doesn't change that. So, remove the three fopens and put a new one after the last if statement followed by a fprintf(fp, "%d %d %d", stdsuite, premsuite, deluxesuite); to do the actual writing. then finish with an fclose(fp) again

Related

Reading integers from a file in C using a while-loop and getw(f) != EOF

I pretty new to C, or well, very new to C. I'm trying to write integers to a file using putw(), and then I try to read them using getw(), I read them using a while loop until EOF. But the loop dies prematurely, and it seems to do so when getw() gets the integer 26 from the file. I'm at a complete loss.
Basically I want to printf the integers that I previously saved to the file, using putw(), every 7th iteration I print a new line. It works all the way until getw() encounters the integer 26, that kills the loop, even if it isnt EOF. No matter how many integers I have in the file, it works only until getw() encounters 26. I´ve tried using fscanf but didnt get that to work either. Please help a beginner.
void readfile() {
FILE *f;
f = fopen("INTEGERS.DAT", "r");
int num, xar=1;
if (f==NULL){
printf("NO file detected.\n");
exit(0);
} else {
while((num = getw(f)) != EOF) {
printf("%d ", num);
if ( xar % 7 == 0) {
printf("\n");
}
xar++;
}
}
fclose(f);
}
Thanks in advance.
You didn't indicate the format of your data file, but noting that you are opening the file with an "r" parameter, that would indicate that the data in the file is in a text format and not a binary format. So using that information and a bit of artistic license, I created a code snippet to build some text data with an integer value per line/record in a file, and then read the data in that file utilizing a tweaked version of your readfile function.
#include <stdio.h>
#include <stdlib.h>
void save_int(void)
{
int entry = 999;
FILE *fp;
fp = fopen("INTEGERS.DAT", "w");
if (fp != NULL)
{
while (1)
{
printf("Enter an integer or enter '0' to quit data entry: ");
scanf("%d", &entry);
if (entry == 0)
{
break;
}
fprintf(fp, "%d\n", entry);
}
}
fclose(fp);
return;
}
void readfile()
{
FILE *fp;
fp = fopen("INTEGERS.DAT", "r");
char number[16];
int value;
if (fp==NULL)
{
printf("NO file detected.\n");
exit(0);
}
else
{
while(1)
{
value = fscanf(fp, "%s", number);
if (value < 0)
{
break;
}
printf("%d ", atoi(number));
}
}
printf("\n");
fclose(fp);
}
int main()
{
save_int();
readfile();
return 0;
}
Some items to point out.
Each integer value is being written with a newline character to the text file, so that would be a caveat if your file actually is in a different format such as storing integers on the same line with some type of delimiter between the integer values.
In reading in the integer data from the created text file, fscanf is used for this task - you might get suggestions and other answers utilizing other functions such as fgets. There are pros and cons, so often it comes down to what is most familiar and comfortable to you.
Since the values were stored as string values, they are read in to a string and then converted to an integer utilizing the standard atoi function. Again, this is just a simple way to do this that I am familiar with. By all means, view any alternative answers you might get and/or comments added later to this answer.
With that, following is some sample output at the terminal.
#Dev:~/C_Programs/Console/Integers/bin/Release$ ./Integers
Enter an integer or enter '0' to quit data entry: 14
Enter an integer or enter '0' to quit data entry: 566
Enter an integer or enter '0' to quit data entry: 65335
Enter an integer or enter '0' to quit data entry: 122
Enter an integer or enter '0' to quit data entry: 18
Enter an integer or enter '0' to quit data entry: 0
14 566 65335 122 18
#Dev:~/C_Programs/Console/Integers/bin/Release$ cat INTEGERS.DAT
14
566
65335
122
18
Go ahead and test this out to see if it meets the spirit of your project.

C - Price look-up program

I was asked to make a C program that act as a 'price lookup' where a user enter a product name and the program will print it's name and price which is stored in a file. If the item is not present in the file, the program will let the user know. The program will keep looping as long as the user wants to search. I did the coding using Dev C++, however after i run the code, the program got stuck after a few loops, and it's random. Could you guys detect any problem with my coding, or is it just the problem with Dev C++? I include my code below. Your help is greatly appreciated.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
int main()
{
FILE *items;
char *mode="r";
char pName[50];
float pPrice;
char p1Name[50];
int value=0;
char respond='Y';
char s[50];
items=fopen("Product_Name_Price.txt", mode);
if(items==NULL)
{
fprintf(stderr, "Can't open file Product_Name_Price.txt!\n");
exit(1);
}
printf("File has been successfully opened\n");
do
{
printf("Enter the name of the product you wish to look for\n");
scanf("%s", &p1Name);
while(strcmp(p1Name, pName) !=0)
{
fscanf(items,"%s %f", pName, &pPrice);
//printf("%s\t%.2f\n", pName, pPrice);
//value=strcmp(p1Name, pName);
if(strcmp(p1Name, pName) == 0)
{
printf("%s\t%.2f\n", pName, pPrice);
}
}
/*
else
{
printf("No data in system\n");
}
*/
printf("Do you wish to look up for more item? (Y/N)\n");
scanf("%s", &respond);
}while(respond=='Y'|| respond=='y');
printf("This program is closing\n");
fclose(items);
}
Your program has undefined behaviour because your scanf("%s", &response) reads into response as if it were an array of sufficient size for the string being read — that size is at least 2 (including null terminator), but response is just one character. You blew up your stack and corrupted memory and then all bets are off.
You could write scanf("%c", &response) instead to actually read a single character, though you'd be better off switching to modern, safer tools if you're writing a C++ program.

While writing into a file , Where intermediate data stored in between fopen() and fclose()?

Below is a small program which takes information from user and write it into a file teacher.txt.
I am using only one array q2[30] for taking input and writing into a file using fprintf().
But when i want to enter more teacher then again loop will execute but at this time fclose() will not appear
so data will not be write/save(don't know) into file also previous value of q2 get erased/overwrite with new input.
So in this case where data is stored/write by fprintf().Because when i manually open teacher.txt before fclose() there is no any new data.
#include <conio.h>
#include <iostream.h>
int main()
{
system("cls");
int yoe;
char cond[]="yes";char q2[30];
FILE *p;
p = fopen("teacher.txt","a+"); //opening file in reading + appending mode
printf("\nDo you want to add more Teacher ? (yes/no)\n");
gets(q2);
fflush(stdin);
if(!strcmp(q2,cond))
{
do
{
printf("\nEnter Teacher's Name\n");
gets(q2);
fprintf(p,"\n!%s!",q2);
printf("Enter Teacher's Qualifications\n");
fflush(stdin);
gets(q2);
fprintf(p,"%s!",q2);
printf("Enter Teacher's year of experience (0-30)\n");
fflush(stdin);
scanf("%d",&yoe);
fprintf(p,"%d!",yoe);
printf("Enter Teacher's Mobile number(id) [Should be of 10 digits]\n");
fflush(stdin);
gets(q2);
fprintf(p,"%s!",q2);
printf("\nDo you want to add more Teacher ? (yes/no)\n");
fflush(stdin);
gets(q2);
}while(!strcmp(q2,cond)); // condition to check , if user want to add more Teacher , if yes then loop will execute again.
fclose(p); // when user enter 'no' then only fclose will appear.
}
fclose(p);printf("\nPress any key to return to Admin menu\n");
getch();
system("pause");
}
When you open a file with fopen, the output you write is buffered, meaning it's not actually send to the lower layers until the buffer is either full or you explicitly flush it with fflush.
Also note that the layers under fopen/fprintf/fclose, all the way down to the actual hardware, may also have some buffering that can delay the actual update of the on-disk data.

Save increment in C

I need a little help with something I'm writing C.
So I have this:
int a;
int one = 4;
int two = 3;
printf("If you have done this then write '1', if you have done that write '2' ");
scanf("%d" ,&a);
if (a=1)
{
one = one+1;
}
else if (a=2)
{
two = two+1
}
So the thing I want to do is to save that increment so the next time I open the program, the values of int one and two will change based on the last usage of the program.
How do I do this ?
Use fopen/fread/fclose when you start the application (before write checks if the FILE handle is correctly created)
Use fopen/fwrite/fclose when you exit from the application.
How to access file? very basic functionalities, read this:
http://www.thegeekstuff.com/2012/07/c-file-handling/
When the program finishes processing it is removed from the memory along with it's variables. What you what to achieve requires file handling. See this tutorial - http://www.w3schools.in/c/file-handling/
A couple of suggestions:
1 - It's supposed to be if (a==1)
2 - It's always recommended to add the else block for a if..else if control block to handle any unexpected input
Read both variables from file and write it to the file before closing the program.
#include <stdio.h>
int main(void)
{
int a;
FILE *fp = fopen("save.txt", "r+"); // File contains two integers 4 3
int one;
int two;
fscanf(fp, "%d %d", &one, &two);
printf("%d %d\n", one, two);
printf("If you have done this then write '1', if you have done that write '2' ");
scanf("%d" ,&a);
if (a==1)
{
one = one+1;
}
else if (a==2)
{
two = two+1;
}
rewind(fp);
fprintf(fp,"%d %d\n", one, two);
}

Creating a file and copying its content to other file in c language

I need a C program to copy contents of one file to another file along with the following conditions :
1.) The file from which we read data may or may not exist.
2.) The file to which the data is being copied may or may not exist.
If the file exists, then the data shall be directly copied and if the file doesn't exist, there should be an option to create the file ,enter data into it and the later copying the contents of the file into the other file.
I worked out the following code,
At present my modified code is :
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *f1,*f2;
char c;
char name1[20],name2[20];
clrscr();
setvbuf(stdout, 0, _IONBF, 0);
printf("\nEnter the source file name :");
scanf("%s",name1);
if((f1=fopen(name1,"r"))==NULL)
{
fclose(f1);
f1=fopen(name1,"w");
printf("\nThe specified file does not exist \nNew file will be created");
printf("\nEnter the data for the new file : ");
while((c=getchar())!=EOF)
{
putc(c,f1);
}
fclose(f1);
}
f1=fopen(name1,"r");
printf("\nEnter the destination file name :");
scanf("%s",name2);
f2=fopen(name2,"w+");
while((c=getc(f1))!=EOF)
{
putc(c,f2);
}
rewind(f1);
rewind(f2);
printf("The data in the source file is :\n");
while((c=getc(f1))!=EOF)
{
printf("%c",c);
}
printf("\nThe data in the destination file is :\n");
while((c=getc(f2))!=EOF)
{
printf("%c",c);
}
fclose(f1);
fclose(f2);
fflush(stdin);
getch();
}
But the program works fine only when the source file already exists . If i create a new file then no input is being taken for the destination file name and the data in the destination file file is blank. So what should i do now?
What should I modify to make it work ?
Suggest me any code of your choice also ... Thank you !
Well, I see several problems in your code but the most important one is that the expression:
(c=getc(f1))!=EOF
Will always evaluated true, so you'll run infinite loops. If you read getc documentation you'll realize that it returns an int not a char:
getc documentation
So basically what you're doing is truncating the EOF, which is an int generally defined as -1, to a char when:
c=getc(f1) // At this point C = 255(0xFF) if getc returned EOF
And then promoting c to an int when is compared to EOF, since an int is big
enough to hold 255 the comparison made is 255 != -1, which is always true.
To fix that just declare c as an int.
Some more tips:
You may also want to make sure that it was an end of file condition by using feof since getc returns EOF on other error conditions as well.
You may want to move the "\n" to the end of your senteces on printf calls to force a flush on stdout. Or alternatively you can add this at the start of your program:
setvbuf(stdout, 0, _IONBF, 0);
It seems you're storing the file names in name1 and name2, so you may want to remove the double quotes from fopen file names so that it actually use them.
When you open name2 you use write access only but at the end you try to read and display its content, you may want to use "w+" as the access mode.
When you're trying to print the resulting data the file handler in f2 has already been closed. And the file cursors are already at the end of the file so you may want to use rewind, e.g.
//fclose(f2);
rewind(f1);
rewind(f2);
printf("The data in the source file is :\n");
while((c=getc(f1))!=EOF)
{
printf("%c",c);
}
printf("The data in the destination file is :\n");
while((c=getc(f2))!=EOF)
{
printf("%c",c);
}
fclose(f1);
fclose(f2);
finally the successful code is :
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *f1,*f2;
char c;
char name1[20],name2[20];
clrscr();
printf("\nEnter the source file name :");
scanf("%s",name1);
if((f1=fopen(name1,"r"))==NULL)
{
fclose(f1);
f1=fopen(name1,"w");
printf("\nThe specified file does not exist \nNew file will be created");
printf("\nEnter the data for the new file : ");
while((c=getchar())!='^')
{
putc(c,f1);
}
fclose(f1);
}
f1=fopen(name1,"r");
printf("\nEnter the destination file name :");
scanf("%s",name2);
f2=fopen(name2,"w+");
while((c=getc(f1))!=EOF)
{
putc(c,f2);
}
rewind(f1);
rewind(f2);
printf("The data in the source file is :\n");
while((c=getc(f1))!=EOF)
{
printf("%c",c);
}
printf("\nThe data in the destination file is :\n");
while((c=getc(f2))!=EOF)
{
printf("%c",c);
}
fclose(f1);
fclose(f2);
fflush(stdin);
getch();
}

Resources