scanf

Get scanf to quit when it reads a newline?

If I input 5 5 at the terminal, press enter, and press enter again, I want to exit out of the loop. int readCoefficents(double complex *c){ int i = 0; double real; double img; while(scanf("%f %f", &real, &img) == 2) c[i++] = real + img * I; c[i++] = 1 + 0*I; // most significant coefficient is assumed to be ...

C Programming Input Error

int main(void) { char *input; printf("prompt>"); scanf("%s", input); printf("%s", input); return 0; } prompt>input RUN FAILED (exit value 138, total time: 3s) What's wrong with the code? Has to be either the scanf() or the second printf(). The input is of unknown length. A lot people have said to simply create a c...

Simple C scanf does not work?

If I try something such as: int anint; char achar; printf("\nEnter any integer:"); scanf("%d", &anint); printf("\nEnter any character:"); scanf("%c", &achar); printf("\nHello\n"); printf("\nThe integer entered is %d\n", anint); printf("\nThe char entered is %c\n", achar); It allows entering an integer, then skips the second scanf com...

C: How to read only the first word from each line?

Hi, I've done many simple procedures, but I'm only trying to read the first word into a char word[30] , from each line of a text file. I've tried, but without success. Oh, I have to reuse that char each time I read it. (To put in an ordered list each time I read it). Can anyone help me and show me a way to read this way from a file, i...

How do you read scanf until EOF in C?

I have this but once it reaches the supposed EOF it just repeats the loop and scanf again. int main(void) { char words[16]; while(scanf("%15s", words) == 1) printf("%s\n", words); return 0; } ...

How do I read white space using scanf in c?

...

scanf erratic behaviour

I compiled and run the following console program which is supposed to read an integer and return the number of field successfully read: # include <stdio.h> int main ( void ) { int result, number; printf ( " Enter an integer : \n "); result=scanf ( " %d " , & number ); printf ( " Fields read % d " , result ); return 0; } I compi...

C struct with many [char array ] problem.

I have a struct with many char array like this (and it works) : struct maytinh { char tenmay[10]; char mamay[10]; char test[10]; float manhinh; int gia; }; But if its like this, struct maytinh { char tenmay[99]; char mamay[99]; char test[99]; float manhinh; int gia; }; it breaks when I compil...

C: fscanf - infinite loop when first character matches

I am attempting to parse a text (CSS) file using fscanf and pull out all statements that match this pattern: @import "some/file/somewhere.css"; To do this, I have the following loop set up: FILE *file = fopen(pathToSomeFile, "r"); char *buffer = (char *)malloc(sizeof(char) * 9000); while(!feof(file)) { // %*[^@] : Read and discar...

Understanding scanf() in C (Seg Fault)

I don't understand getting input in C. I have the following code which is producing a segmentation fault: int main(int argc, char *argv[]){ while (fgets(buffer, MAX_LEN + 1, input) != NULL) { get_command(t, buffer); } return 0; } and static void get_command(Table *t, char *command) { COMMAND command_name = 0; char *valid_...

Scanf and loops

here a a piece of code that is supposed to loop over and over until the user inputs a number between 0 and 15 int input; do { printf("Enter a number => "); scanf("%d",&input); printf("\n %d \n",input); } while (!(input>0 && input <15)); However ...

Why scanf must take the address of operator

As the title says, I always wonder why scanf must take the address of operator (&). ...

C++ scanf/printf of array

I've written following code: int main() { double A[2]; printf("Enter coordinates of the point (x,y):\n"); scanf("%d,%d", &A[0], &A[1]); printf("Coordinates of the point: %d, %d", A[0], A[1]); return 0; } It's acting like this: Enter coordinates of the point (x,y): 3,5 Coordinates of the point: 3, 267...

Reading a specific number of lines from a file in C (scanf, fseek,fgets)

I have a process master that spawns N child processes that communicate with the parent through unnamed pipes. I must be able to: make the father open the file and then send, to each child, a struct telling that it has to read from min to max line; this is going to happen at the same time, so I don't know: 1st how to divide total_lines ...

Not sure why I have "EXC Bad Access" error.

Hello everyone! I've programmed for a while in Java and .Net, but never really used C or Objective C. I'm still trying to understand a few concepts. I was working on a simple program just to see how I can make an array of structures. Which I believe I got right. I'm having a hard time figuring out how to access the subclasses and store ...

Use of [] in fscanf()

I've a text file with following contents: "abc","def","ghi" The following works to read the file contents properly: int main() { char name[1024] = {0}; FILE *file = fopen("file.txt", "r"); while(1) { if (fscanf(file, " %[\",]s ", name) == EOF) break; if (fscanf(file, " %[a-zA-Z]s ", name) ...

Scanf with spaces does not read at all

#include <stdio.h> int main(){ int i; char name[10],surname[10],id[12],dpart[20]; printf("******** ID Documentation ***********\n\n\n"); printf("1) What is your Name? \n"); scanf("%s",&name); printf("2) What is your Surname? \n"); scanf("%s",&surname); printf("3) What is your ID Number \n"); sc...

Tips on how to read last 'word' in a character array in C

Just looking to be pointed in the right direction: Have standard input to a C program, I've taken each line in at a time and storing in a char[]. Now that I have the char[], how do I take the last word (just assuming separated by a space) and then convert to lowercase? I've tried this but it just hangs the program: while (sscanf(line...

C Program terminating with -1 (0xFFFFFFFF), seemingly no reason?

#include <stdio.h> typedef struct pduct {char name[20]; int price; int stock;} PRODUCT; void init(PRODUCT * product) { printf("What is the name of the product: "); fgets(product->name, 20, stdin); printf("DEBUG: Did it get written...: %s", product->name); printf("What is th...

scanf fails why?

Hi, when I wrote this ,compile and run: int x; scanf ("%d", &x); while (x!=4) { scanf ("%d", &x); } and when inserting char or double number less than 4 it enter an infinite loop. when inserting double greater than 4 it terminates. Any explanation? ...