I don't know why it is writing only one line in my file
void foo()
{
int ID;
char answer;
FILE *input = fopen("Dane.txt", "w");
do
{
printf("Give ID: ");
scanf("%d",&ID);
fprintf(input, "%d\n", ID);
printf("Exit? y/n ");
scanf("%s", &answer);
fflush(NULL);
}
while (answer != 'n');
fclose(input);
}
Output (in file) is only first ID number which I write on console. But where are others?
EDIT: ok I got it. The error was in char answerand it should be char answer[2] and ending while should be while(answer[0] != ...). Before it the program read only one character - the line end. When i hit e.g. "n ENTER" it take only ENTER. Now it take the first char from tab i.e. "n". Thank everybody for help
You are doing some logical mistake. You are asking whether exit or not. If user does not want to exit, then he would press n. So, to continue the loop, the answer should be equal to n, right?
Modified version of your program:
void foo()
{
int ID;
char answer;
FILE *input = fopen("Dane.txt", "w");
do
{
printf("Give ID: ");
scanf("%d",&ID);
fprintf(input, "%d\n", ID);
printf("Exit? y/n ");
scanf(" %c", &answer);
fflush(NULL);
}
while (answer == 'n');
fclose(input);
}
answer has only one space to read and it isn't capable to store string whose length is 1 character or longer.
This won't affect the result, but using input for output file pointer is confusing.
The conditio in while is unnatural.
Try this:
#include <stdio.h>
void foo();
int main() {foo(); return 0;}
void foo()
{
int ID;
char answer[4];
FILE *output = fopen("Dane.txt", "w");
if (output == NULL) return;
do
{
printf("Give ID: ");
if (scanf("%d",&ID) != 1) break;
fprintf(output, "%d\n", ID);
printf("Exit? y/n ");
if (scanf("%3s", answer) != 1) break;
fflush(NULL);
}
while (answer[0] != 'y');
fclose(output);
}
When I ran your function, I got all three numbers I entered in the file:
$ ./a.out
Give ID: 25
Exit? y/n y
Give ID: 33
Exit? y/n y
Give ID: 10
Exit? y/n n
$ cat Dane.txt
25
33
10
However, your question is backwards. You ask, "Exit? y/n" and then exit if the answer is "n" ("no"). The question should be "Continue? y/n", so that when the user answers in the affirmative, it continues.
Also, naming your output filehandle "input" is backwards, and as others have mentioned, your answer variable should be a character array of at least 2 characters, as char answer[2];.
Related
I'm new to C, and I was trying to create a password system with 2 questions. 1. "What is your password?" I prompted the user to answer and recorded the argument. Any input was assigned to the variable. 2."Are you sure? Do you want this password? Enter Yes or No." It would decide whether to use the answer or discard it. Everything was working smoothly until this part. I couldn't type Yes or No. It automatically assumed that I had typed yes and saved it. Can somebody please give me some advice?
#include <stdio.h>
int
main ()
{
char password;
printf ("Hello User.");
printf ("Please type in your password:");
scanf ("%d", & password);
char answer;
printf ("\nAre you sure? Do you want this password? Enter Yes or No: \n");
scanf ("%c", answer);
printf ("\nAnswer is %c");
if (answer == 'Yes')
printf ("Confirmed.");
else (answer == 'No');
printf ("OK. Thank you.");
password = 0;
return 0;
}
As noted in the comments, you likely didn't intend to store a password in a single character. Nor did you likely intend to scanf the password as an integer using, which is what you're saying with %d.
You may want to use getline instead to read an entire line, and store it in password if you declare it as char array.
Let's say you gave it room for 100 chars.
char password[100];
You might read your password in with:
getline(password, 100, stdin);
You can do the same for the second question, but should always use strcmp to compare strings in C. When the two strings are equal, the return value is 0. Be careful as this is opposite of the way that "true" is usually represented by integers in C.
if (strcmp(response, "yes") == 0) {
...
}
Hopefully these tips will point you in the right direction.
Research and study the functions and syntax.
#include <stdio.h>
#include <string.h>
//function: Clears password
void clearPassword(char p[])
{
for(int i = strlen( p ); i>=0; i--)
{
p[i]='\0';
}
}
int main ()
{
char password[101]; //C version of a String
char line[100]; //C version of a String
char answer;
printf ("Hello User.");
printf ("Please type in your password: ");
fgets(line, sizeof(line), stdin); // read input
sscanf(line, "%s", &password); // store password
printf ("\nAre you sure? Do you want this password? Enter Y or N: \n");
fgets(line, sizeof(line), stdin); //read input
sscanf(line, "%c", &answer); // store answer
printf ("\nAnswer is %c \n", answer); // print answer
if (answer == 'Y') //
printf ("Confirmed.");
else
{
clearPassword(password);
printf ("OK. Thank you.");
}
return 0;
}
I've just started learning the language of C, and would love your help in cleaning up / simplifying my code if you know a better way to reach the following.
I want a program to ask for a number, and if that is found then proceed to print and end, however if anything else is put in (e.g. a letter key), then I want the program to loop asking for a number until one is given.
I started off by using a simple scanf input command, but this seemed to go into an infinite loop when I tried to check if a valid number (as we define them) was put in.
So instead I have ended up with this, from playing around / looking online, but I would love to know if there is any more efficient way!
//
// Name & Age Program
// Created by Ben Warren on 1/3/18.
//
#include <stdio.h>
int main (void)
{
//Setting up variables
int num;
char line[10]; /* this is for input */
//Collecting input
printf("Please enter any number? \t");
scanf("%d", &num);
//If Invalid input
while (num==0)
{
printf("\nTry again:\t");
fgets(line, 10, stdin); //turning input into line array
sscanf(line, "%d",&num); //scaning for number inside line and storing it as 'num'
if (num==0) printf("\nThat's not an number!");
}
//If Valid input
{
printf("\n%d is nice number, thank you! \n\n", num);
*}*
return 0;
}
Instead of checking if the value is different to 0, check the return value of
sscanf. It returns the number of conversions it made. In your case it should be 1. Unless the return value is 1, keep asking for a number.
#include <stdio.h>
int main(void)
{
int ret, num;
char line[1024];
do {
printf("Enter a number: ");
fflush(stdout);
if(fgets(line, sizeof line, stdin) == NULL)
{
fprintf(stderr, "Cannot read from stdin anymore\n");
return 1;
}
ret = sscanf(line, "%d", &num);
if(ret != 1)
fprintf(stderr, "That was not a number! Try again.\n");
} while(ret != 1);
printf("The number you entered is: %d\n", num);
return 0;
}
That is not a bad approach for someone new to C. One small improvement would be to actually check the return value of scanf(), since it returns the number of arguments successfully retrieved. Then you could get away from relying on num being 0 to indicate the input was valid. Unless you do want to specifically flag 0 as invalid input.
int ret = scanf("%d", &num);
ret == 1 would mean an integer was succesffully read into num, ret == 0 would mean it was not.
Consider using strtol to parse a string for a long int. This also allows you to detect trailing characters. In this example if the trailing character is not a newline, the input can be rejected. strtol can also detect overflow values. Read the documentation to see how that works.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
//Setting up variables
long int num = 0;
char line[40] = ""; /* this is for input */
char *parsed = NULL;
printf("Please enter any number? \t");
fflush ( stdout);
while ( fgets(line, 40, stdin))
{
parsed = line;//set parsed to point to start of line
num = strtol ( line, &parsed, 10);
if ( parsed == line) {//if parsed equals start of line there was no integer
printf("Please enter a number? \t");
printf("\nTry again:\t");
fflush ( stdout);
continue;
}
if ( '\n' != *parsed) {//if the last character is not a newline reject the input
printf("Please enter only a number? \t");
printf("\nTry again:\t");
fflush ( stdout);
}
else {
break;
}
}
if ( !parsed || '\n' != *parsed) {
fprintf ( stderr, "problem fgets\n");
return 0;
}
printf("\n%ld is nice number, thank you! \n\n", num);
return 0;
}
0 (zero) is a number...
But I see what you want to do...
You can check for a valid number, using isdigit or a combination of similar functions
I think its also important to follow the advice of other answers to use the return value from scanf using code such as:
int ret = scanf("%d", &num);
and examining ret for success or failure of scanf.
Hello everyone i have to do simple login program in C, i have a problem to save username, full name and password because i need to choose '1' for sign up and '2' for sign in but always when i exit the console and try to read my file everything disappears.
#include <stdio.h>
int main() {
FILE *f = fopen("users.txt", "w+");
if (f == NULL) {
printf("N/A");
exit(1);
}
int choose,
username[15],
fullName[20],
password[15],
// confirmPassword[15];
printf("Welcome!\n");
printf(" 1: Sign up\n 2: Sign in\n");
printf("--------------------------------\n");
scanf("%d", &choose);
if(choose==1) {
printf("Username: ");
scanf("%s", &username);
printf("Full name: ");
scanf("%s", &fullName); /// BECAUSE OF SPACE IT COUNTS LIKE A PASSWORD
printf("Password: ");
scanf("%s", &password);
fprintf(f, "%s\n%s\n%s", username, fullName, password);
}
if(choose==2) {
char c;
printf("Username: ");
while( c != EOF) {
c = fgetc(f);
printf("%c",c);
}
}
fclose(f);
return 0;
}
I have to deal with HASH too but i will try that on my own. And help about /// comment !
As per previous comments, and with my idea, here is how you may fix the code:
1. use int c or unsigned int c to declare c variable.
2. set c tp be (whatever as long it is not EOF);
3. make do while loop that at the end checks for EOF.
My program is designed to sort records in a text file, but my main issue is getting the main function to ask the user if he would like to run the program again to read and write another file. My program right now, when the user enters 1 to run again, skips the first question to enter the program to read. Why is that? I appreciate the help! Here is my main function only: The program compiles only during the first run.
int main()
{
int fieldCount = 0;
int lineCount = 0;
char file[STR_LEN];
char filepath[STR_LEN];
char** fields = NULL;
char** lines = NULL;
int recCount = 0;
Person **sortedRecs = NULL;
int x;
do {
printf("Enter path of the file to read: ");
gets(filepath);
printf("Enter path to copy to: ");
gets(file);
fields = readFieldsDynamic(filepath, &fieldCount);
lines = readLinesDynamic(filepath, &lineCount);
recCount = getPersons(fields, fieldCount, &sortedRecs);
if (recCount && lines && sortedRecs && (recCount <= lineCount)) {
writeRecsToFile(file, sortedRecs, recCount, lines, lineCount);
printf("Sorted records are written in %s\n", file);
}
if (fields) {
freePointerToChars(fields, fieldCount);
}
if (lines) {
freePointerToChars(lines, lineCount);
}
if (sortedRecs) {
freeRecs(sortedRecs, recCount);
}
printf("Enter 1 to run program again: ");
scanf("%d%*c", &x);
} while (x == 1);
return 0;
}
What you can do is add a while loop to "eat up" all the newlines left in stdin stream to prevent the next getchar to not block for real user input.
while ((ch=getchar()) != EOF && ch != '\n')
;
Also please don't use gets in your code. Try fgets instead.
You need to keep a space before %c.
int main()
{
char ch;
do
{
something();
printf("\nDo you want to continue?");
scanf(" %c", &ch);
}while(ch=='y');
return 0;
}
This answer is based on your output looking something like:
Enter path of the file to read: /my/input/here/
Enter path to copy to: /blah/copy/here/
Enter 1 to run program again: 1
Enter path of the file to read:
Enter path to copy to: help/I/cant/type/in/the/above/field
My understanding is your program is probably carrying over the newlines between loops.
I've had similar problems in C++, and placing cin.get() after an input fixed it. I'm not certain of the C equivalent.
I'm creating a program that should create a structure of a list of people entered by the user; the only problem I'm having is getting the user input data to appear in the text file. Anyone know how to do this? Here is the code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct person{
char name[20];
int age;
struct person *next_ptr;
} PERSON;
int main (void){
struct person PERSON;
FILE *fp;
char ans, ch;
int ppl=0;
fp=fopen("person_struct", "w");
if(fp != NULL){
while(ppl<25){
printf("Would you like to add a person to the list? [y/n] ");
scanf("%c", &ans);
if(ans == 'y') {
printf("\nEnter a name:\n");
scanf("%s", PERSON.name);
fprintf(fp, "%s",PERSON.name);
printf("\nEnter age:\n");
scanf("%i", &PERSON.age);
fprintf(fp, " %i\n", PERSON.age);
}
else {
ppl=25;
}
ppl++;
}
fclose(fp);
}
printf("\n\n\n");
system("pause");
return 0;
}
Youe scanf statement is wrong you forgot ampersand & operator before PERSON.age its int
scanf("%i", PERSON.age);
^ & missing
correct is:
scanf("%i", &PERSON.age);
You have two scanf stamens in your code to inputs from user one for string to scan name.
scanf("%s", PERSON.name);
This is correct and No need of & before string. But age is int and to scan int.float you need to add & before variable that is why added ampersand & before PERSON.age.
ref: scanf
Second:
fputs(PERSON.age, fp); is wrong syntax of fputs is:
int fputs( const char *str, FILE *stream );
^ you are passing int
first argument should be const char* but your are passing int
fputs(PERSON.age, fp);
^ wrong , age is int not char*
When you need formatting input/output prefer printf and scanf functions, My suggestion change your read/write like: (read comments)
printf("Enter a name:\n");
scanf("%s", PERSON.name); // here is No & because `name` is string
scanf("%i", &PERSON.age); // age is `int` so & needed
fprintf(fp,"%s %i\n",PERSON.name, PERSON.age);
EDIT: Because you commented, your code is working after these rectifications, see
$ gcc x.c -Wall
$ ./a.out
Would you like to add a person to the list? [y/n]y
Enter a name:
yourname
14
Would you like to add a person to the list? [y/n]y
Enter a name:
firendName
15
Would you like to add a person to the list? [y/n]n
sh: 1: pause: not found
$ cat person_struct.txt
yourname 14
firendName 15
In addition to Grijesh's answer:
Please explain scanf("%s", &ans);. How many characters can you store in ans? How many characters does the string "y" require to store? Verify your beliefs: printf("sizeof ans: %zu\n" "sizeoof \"y\": %zu\n", sizeof ans, sizeof "y");
Perhaps you meant: if (scanf("%c", &ans) != 1) { /* assume stdin has closed or reached EOF */ }. Note the %c, which will read only one character into ans.
Alternatively, if you change ans to an int, you can use: ans = getchar();
edit: In short, I think your loop should look something like this:
for (size_t ppl = 0; ppl < 25; ppl++){
int ans;
printf("Would you like to add a person to the list? [y/n]");
do {
ans = getchar();
while (ans >= 0 && isspace(ans));
if (ans != 'y') {
break;
}
printf("Enter a name:\n");
if (scanf("%s", PERSON.name) != 1 || scanf("%i", &PERSON.age) != 1) {
break;
}
fprintf(fp, "%s %i\n", PERSON.name, PERSON.age);
}