How do I write a C program to enter a number from the keyboard and store it in a text file called number.dat. If the number exists in the file, display an error message. Program should allow to input numbers until the user inputs -99.
I tried this question, but I could not find how to check whether the integer already exists in the file.
This is my C code answer:
#include <stdio.h>
int main(void) {
int num;
FILE *xPtr;
xPtr = fopen("number.dat", "a");
while (num = -99) {
printf("Enter a number : ");
scanf("%d", &num);
if (num == -99)
break;
fprintf(xPtr, "%d\n", num);
}
return 0;
}
I could not find how to check whether an integer already exists in the file.
Can you please give me a solution for this matter?
There are a few things that could be improved. First of all, your while loop is using an assignment operator for the conditional expression:
while(num = -99)
Instead of assigning it to 99 at the beginning of the loop each time, you could do this:
while(num != -99)
Since you want the block inside of the loop to execute when num is not equal to -99. I would also convert it to a do-while loop, since you are not assigning num before you enter the loop the first time. When you are in the loop, you should be opening an closing the file so it can keep track of any numbers you add. In order to check your file you will need to read AND append, it is now currently set to only append. To read and append you will do the following:
xPtr = fopen("number.dat", "a+");
It might be helpful to add a function called has_num or whatever you choose, which takes the FILE* and checks for an int returning 1 if found and 0 if not. So you will modify your if statement to be
if (num != -99 && !has_num(xPtr,num))
{
fprintf(xPtr, "%d\n", num);
}
So with all of those changes, your code will become
#include <stdio.h>
int has_num(FILE* file, int num)
{
int curr;
int fnd = 0;
while (!fnd && (fscanf(file, "%d\n", &curr) != EOF))
{
fnd = (curr == num);
}
return fnd;
}
int main(void)
{
int num;
FILE *xPtr ;
do
{
xPtr = fopen("number.dat", "a+");
printf("Enter a number : ");
scanf("%d", &num);
if (num != -99 && !has_num(xPtr,num))
{
fprintf(xPtr, "%d\n", num);
}
fclose(xPtr);
} while (num != -99);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fp; //create a pointer to FILE
char c,n,num;
printf("Enter a Number : ");
scanf("%c",&n);
if(n== -99){
// printf("Enter number greater than -99");
exit(0);
}
else{
fp = fopen("numbers.dat", "r+"); //read file
while ((c = getc(fp)) != EOF) {
num = c;
if(num==n){
printf("Included Number");
exit(0);
}
}
fp = fopen("numbers.dat","w+"); //write new number to file
putc(n, fp);
}
}
You program should successfully append numbers typed by the user to the file, but it has a few problems:
you do not check if fopen() successfully opened the file, you have undefined behavior if fopen() returns NULL.
you do not check if scanf() encountered an input failure. Entering a character that os not a number will cause undefined behavior, most likely an infinite loop appending the previously entered number if any.
you do not close the file. This will not have any adverse effect because all files are automatically closed upon program exit, but it is good style to close them explicitly.
of course you do not check if the number already exists in the file.
For the last problem, you should fopen() the file for both read and write, rewind the file and read all lines, checking if the number is present.
Here is a modified version:
#include <stdio.h>
int main(void) {
int c, num, num1, found;
FILE *xPtr = fopen("number.dat", "w+");
if (xPtr == NULL) {
printf("cannot open file number.dat\n");
return 1;
}
for (;;) {
printf("Enter a number: ");
if (scanf("%d", &num) != 1) {
if (feof(stdin))
break;
printf("invalid input\n");
while ((c = getchar()) != EOF && c != '\n')
continue;
if (c == EOF)
break;
} else {
if (num == -99)
break;
rewind(xPtr);
found = 0;
for (;;) {
/* skip all characters that cannot start a number */
if (fscanf(xPtr, "%*[^-+0-9]") == EOF)
break;
if (fscanf(xPtr, "%d", &num2) == 1) {
if (num == num2) {
found = 1;
break;
}
} else {
getc(xPtr); // consume a byte
}
}
if (found) {
printf("number is already in the file\n");
} else {
/* append the number */
fseek(xPtr, 0L, SEEK_END);
fprintf(xPtr, "%d\n", num);
}
}
}
fclose(xPtr);
return 0;
}
Related
I can't get my delete record function to break out of the switch case or even give me an error when the record doesn't exist. Could someone please tell me why this is?
Any help is much appreciated!
I can't get my delete record function to break out of the switch case or even give me an error when the record doesn't exist. Could someone please tell me why this is?
Any help is much appreciated!
void delete_record();
void displayContent();
struct Update
{
char studentName[50];
char studentID [50];
char emailID[100];
char courseID[5];
char grade[50];
} update2;
int main ()
{
int num;
do
{
printf("1. Delete a record for the specific name\n");
printf("2. Display Content of File\n");
printf("6. Exit\n");
switch(num)
{
case 1:
printf("this is a test\n");
delete_record();
break;
//displayContent();
//printf("this is a test 2\n");
case 2:
printf("\n\nDiplaying Contents of File\n\n");
displayContent();
default:
printf("Give me a break!\n");
break;
}
scanf("%d", &num);
} while (num != 6);
return 0;
}
void delete_record()
{
FILE *fp;
FILE *fp_tmp;
fp = fopen ("BINARY_FILE.txt", "w");
char studentsID[20];
printf("enter studentID to delete:");
scanf("%s",studentsID);
printf("is this a test?\n");
while(fread(&update2,sizeof(update2),1,fp))
{
printf("this is another test\n");
if(strcmp(update2.studentID,studentsID) != 0)
{
//printf("testing\n");
fwrite(&update2,sizeof(update2),1,fp);
}
else
{
printf("No student with that student ID\n");
}
}
printf("more tests\n");
fclose(fp);
return;
}
void displayContent()
{
char c;
// Open file
FILE *fp;
fp = fopen ("BINARY_FILE.txt", "r");
if (fp == NULL)
{
printf("File Has No Content\n");
exit(0);
}
// Read contents from file
c = fgetc(fp);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fp);
}
fclose(fp);
//return 0;
}
When using scanf to read from the keyboard you must remember
that all characters that you enter are written to the in-buffer,
this means that if you type in
42ENTER
The ENTER will also be present in the in-buffer, so next time you call scanf ENTER will still be in the buffer and then scanf returns 0 since the format specificier "%d" doesn't match.
The easiest way to handle input from keyboard in C and to avoid the hassle of scanf in-buffer by using fgets() to read from the keyboard, then use sscanf() to cherry pick from the buffer:
// always check return value from all runtime functions when possible
char buffer[128];
if (fgets(buffer,sizeof(buffer), stdin) != NULL)
{
if (sscanf(buffer, "%d", &num) == 1)
{
}
else {...}
}
else {...}
after tirelessly looking for an explanation I've decided to ask the greats at stackoverflow. So I'm currently trying to read each input line by line from a file called data.txt. The program works perfectly fine using simple scanf and such, but when I want to read input values from a file the program only reads the first 3 lines of the txt and it keeps repeating in an infinite loop. My code is shown below. I kept out the majority of my code in case others may be tempted to use it. Program will just read 1, 12, 0 infinitely. sample data.txt file is shown below
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
// Global variables
char *Hstring = NULL;
int maxLength, parity;
char *temp = NULL;
int userChoice = 0;
void option1() {
// User inputs length and even or odd parity bit
printf("\n*** Maximum Code Length: %d", maxLength);
//scanf("%d",&maxLength);
printf("\n*** Parity: %d", parity);
//scanf("%d",&parity);
// allocate memory for hamming string based on maximum length and
//size of character element
Hstring = (char *)malloc(maxLength * sizeof(char));
return;
}
void option2() {
/* declare local vars */
int aLength, k, parBits, parValue, errorPos, i, j;
/* prompt for hamming code as a "string"*/
printf("\nEnter the Hamming Code: ");
scanf("%s", Hstring);
temp = Hstring;
aLength = strlen(Hstring);
parBits = ceil(log(aLength) / log(2));
}
int main() {
FILE *fp;
fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("ERROR OPENING THE FILE\n");
}
fscanf(fp, "%d %d %d", &userChoice, &maxLength, &parity);
//end file open
while (userChoice != 3) {
printf("\nEnter Selection: %d", userChoice);
//scanf("%d",&userChoice);
switch (userChoice) {
case 1:option1();
break;
case 2:option2();
break;
case 3:
printf("\n*** Program Terminated Normally\n");
break;
default: printf("invalid input please input another number\n\n");
break;
}
}
/* print out menu, prompt for choice, and call appropriate procedure
until user quits */
return 1;
}
SAMPLE data.txt
1
12
0
2
1000
1
Code starts to loop when it reads the third integer(parity) in option1()
Any help would be appreciated. Thank you!
You never modify userChoice in your while loop, so it's gonna loop forever.
Anyways, even if you were using fscanf in the while loop, and therefore reading the whole file until you find userChoice == 3, it's a bad idea to have your loop termination condition only depending on the content of a file, you should also check the result of fscanf for termination of the file. Your example data would still loop forever because it contains no 3.
The answer here https://stackoverflow.com/a/53475412/4386427 is correctly describing the problem, i.e. that you have an endless loop because userChoice is only read once.
Here is a suggestion for a fix.
int main() {
FILE *fp;
fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("ERROR OPENING THE FILE\n");
}
while (userChoice != 3) {
// Check that exactly 3 items are read from the file.
// If not terminate the program
if (fscanf(fp, "%d %d %d", &userChoice, &maxLength, &parity) != 3) {
printf("Illegal input from file or EOF. Terminating program\n");
break;
}
switch (userChoice) {
case 1:option1();
break;
case 2:option2();
break;
case 3:
printf("\n*** Program Terminated Normally\n");
break;
default: printf("invalid input please input another number\n\n");
break;
}
}
return 1;
}
I seem to be having an issue with some code. The purpose of the code is to take a phrase and convert it to pig latin.
It seems that this code will not take user input, at the block where we say if (x == 1). What it will do is it will automatically take NULL as input to fgets, and I'm clueless as to why.
I've spent entirely too much time on this problem, and I would appreciate any advice on how to improve this code. Please leave a comment on how I can improve my questions in the future.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int pigLatin()
{
char phrase[250] = { 0 };
char pigPhrase[300] = { 0 };
char * tokens[300] = { 0 };
char fileName[260] = { 0 };
FILE * read = NULL;
FILE * write = NULL;
int i = 0;
int x;
int size;
while (i < 10000) {
i++;
x = 0;
printf("Enter one(1) to input via console, two(2) to input via .txt, or (3) to exit:\n");
scanf_s("%d", &x);
if (x == 1) {
printf_s("Enter your Phrase Do not include:\nany punctuation, words less than 2 letters long, or words seperated by blanks:");
fgets(phrase, sizeof phrase, stdin);
phrase[strlen(phrase) - 1] = '\0';
printf_s("\nPhrase Entered:%s\n", phrase);
system("pause");
}
else if (x == 2)
{
printf("Enter name of input file:\n");
scanf_s("%s", fileName, 260);
printf("File name:\n%s\n", fileName);
if (fopen_s(&write, fileName, "r") == 0)
{
scanf_s("%s", phrase, 260);
}
}
else if (x == 3)
{
break;
}
else
{
printf("Invalid Statement\n");
break;
}
}
return 0;
}
scanf("%d", &number); will read an integer but leave everything else in the stream including the '\n' generated by pressing [Enter] after entering the number. This newline left in the stream is then consumed by fgets() without giving you a chance for input.
Clear the stream after using scanf():
int clear(FILE *stream)
{
int ch; // reads until EOF or a newline is encountered:
while((ch = fgetc(stream)) != EOF && ch != '\n');
}
// ...
int number;
if(scanf("%d", &number) != 1) {
// handle error;
}
clear(stdin);
char foo[42];
fgets(foo, sizeof(foo), stdin);
// ...
I am reading from a file, however if it doesn't exist is meant to print 'Read error' but for some reason it is printing read error twice and I don't know why
int loadFlights(char flightDatabase[50], int totalflights, flight_t f[MAX_NUM_FLIGHTS])
{
int counter;
FILE *fp;
fp = fopen("database.txt", "r");
if(fp == NULL) { /************************statement with problem*/
printf("Read error\n");
return 1;
}
fscanf(fp, "%d", &totalflights);
if (totalflights > 5) {
totalflights = 5;
}
for(counter = 0; counter <= totalflights-1; counter++)
{
fscanf(fp, "%s %d %d %d %d %s %d %d %d %d", f[counter].flightcode,
&f[counter].departure_dt.month, &f[counter].departure_dt.date,
&f[counter].departure_dt.hour, &f[counter].departure_dt.minute,
f[counter].arrival_citycode, &f[counter].arrival_dt.month,
&f[counter].arrival_dt.date, &f[counter].arrival_dt.hour,
&f[counter].arrival_dt.minute);
}
fclose(fp);
return totalflights;
}
I've tried putting an if statement around the Read Error if statement saying if its already been printed don't print again however it still seems to be printing.
int main(void)
{
flight_t f[MAX_NUM_FLIGHTS];
int totalflights = 0, menu;
char flightDatabase[50] = "database.txt";
while (menu != 5)
{
print_Menu();
scanf("%d", &menu);
while ((menu < 0) || (menu > 5)) {
printf("Invalid choice\n");
print_Menu();
scanf("%d", &menu);
}
if (menu == 1)
{
addFlight(f, totalflights);
totalflights++;
}
else if (menu == 2)
{
displayFlight(f, totalflights);
}
else if (menu == 3)
{
saveFlight(f, flightDatabase, totalflights);
}
else if (menu == 4)
{
loadFlights(flightDatabase, totalflights, f);
totalflights = loadFlights(flightDatabase, totalflights,f);
}
}
return 0;
}
This is the code where I call on the function.
This is where the problem is:
// Some code
else if (menu == 4)
{
loadFlights(flightDatabase, totalflights, f);
totalflights = loadFlights(flightDatabase, totalflights,f);
}
These are two consecutive calls to loadFlights while the first call doesn't catch the return value. You can get rid of the first one and it should behave the way you expect it to.
Additionally, I see a problem:
while (menu != 5)
At this point, menu is uninitialised, will hold a random value. You might want to either initialise it to zero or 5 or whatever is legal for that data type.
I've tried putting an if statement around the Read Error...
These are the patch works, that are really dangerous to have. Its usually expected to debug the code and find out whats the exact problem rather than adding a patch to cover up an existing bug.
I posted a few days ago already about a problem with my array-program. Well, it is basically a program which lets the user generate an array, safe a specific number in a specific slot, read the array or read a specific slots value. Now I am figuring out, how to let the user save the current array as a file.
this is what I got
void safeFile(){
FILE *f = fopen("list.txt", "a+");
putc(f , list);
fclose(f);
printf("File saved");
start();
}
where's the problem?
My Program crashes everytime I call the function safeFile() .
I played around, came with fputs instead of putc, program won't crash anymore, the file gets created but it's still blank
void safeFile(){
FILE *f = fopen("list.txt", "r+");
fputs(f , list);
fclose(f);
printf("File saved");
start();
}
here is my current full code
would be grateful for advices, im a newb
#include <stdio.h>
#include <stdlib.h>
int list[30];
int x = 0;
int i = 0; // variable used by for-Loop in setList
int j = 0; // variable used by for-Loop in getList
int input;
int option; //start Value
int gvinput; //getValue input
void start();
void getList();
int main()
{
start();
return 0;
}
void setList(int sizeOfList)
{
for (i = x; i <= sizeOfList; i++)
{
list[i] = i;
}
}
void getList(){
for(j = x; j < i ; j++ )
{
printf("At %d we got the value %d \n",j,list[j]);
}
}
void startList()
{
fflush(stdin);
printf("Please enter number between 0 and 30\n ");
scanf("%d",&input);
if(input > 30 || input == 0)
{
printf("The Number is not between 0 and 30\n");
startList();
}
setList(input);
getList();
fflush(stdin);
start();
}
void setValues(int l[])
{
fflush(stdin);
int v;
int loc;
printf("please enter what value you want to safe\n");
scanf("%d",&v);
fflush(stdin);
printf("Where do you want to save it?\n");
scanf("%d",&loc);
l[loc] = v;
printf("we got at slot %d the value %d\n\n",loc,l[loc]);
start();
}
void getValues(int getArray[]){
fflush(stdin);
printf("which slot do you want to read?\n");
scanf("%d",&gvinput);
fflush(stdin);
printf("The value is: %d \n\n",getArray[gvinput]);
start();
}
void safeFile(){
FILE *f = fopen("list.txt", "r+");
fputs(f , list);
fclose(f);
printf("File saved");
start();
}
void start(){
fflush(stdin);
printf("\n");
printf("[L] = generate Slots\n");
printf("[S] = set a Value at specific slot\n");
printf("[G] = get a Value from a specific slot\n");
printf("[F] = safe File");
printf("[X] = exit\n");
option=getchar();
if(option == 'L' || option == 'l'){
startList();
}
if(option == 'S' ||option == 's'){
setValues(list);
}
if (option =='G' ||option == 'g'){
getValues(list);
}
if (option == 'X' || option == 'x'){
printf("Thank you");
}
if (option == 'f' || option == 'F'){
safeFile();
}
int putc( int ch, std::FILE* stream );
putc writes a single char, and you pass an array of ints to it. The same is true for fputs, which writes a null-terminated string. You'll really have to write some code that serializes your data structure to a sequence of bytes to be written to the file.
In your case, since you want to save a list of ints, you can do this with a loop, for example, that is, loop over your array and write each item to the file. You'll also need a function that writes an int (putc isn't good here, since it writes a char). Have a look at printf, if you'd like to stick with C-style IO, otherwise use streams.
If your using putc(), you should be passing char by char.
for(i=0;i<strlen(list);i++)
{
putc(f,list[i]);
}
The list is an array, if you pass the address it means whole array so putc won't work here.
int fputs(const char *s, FILE *stream);
The second argument to the fputs is the stream. Since u use int [10]. u can use fprintf to print to the file.
Also, fflush(stdin); is undefined behaviour
you need to use the loop, but it is not working because the parameters in the putc function are not in the correct order. Try this:
for(i=0;i<strlen(list);i++)
{
putc(list[i],f); //<---------- NOT putc(f,list[i]);
}
first the char and second the stream.
int putc ( int character, FILE * stream );