logo
down
shadow

Reading a file with scanf and a do while loop


Reading a file with scanf and a do while loop

By : Mohammed Nazamul Hoq
Date : November 22 2020, 02:59 PM
will help you I'm reading a file with scanf. Each line is just one data field, so I have a a do while loop. Basically what I do is, read the first information for build the root (i'm using this for build a BST). But when gets inside the do while. Reads weird things. Read and create correct all data, but for each iteration for the loop, call twice times the same method for read a file and I don't what is happening now. , In this code
code :
competitor_node * tmp_node;
tmp_node = calloc(1, sizeof(tmp_node));
scan_status = fscanf(competitor_filename, "%[^\n]\n", tmp_node -> name );
tmp_node = calloc(1, sizeof(*tmp_node));
scan_status = fscanf(competitor_filename, "%[^\n]\n", tmp_node -> name );
tmp_node->name = malloc(MAXSTRINGLENGTH+1);


Share : facebook icon twitter icon
unexpected behavior of while loop variable with scanf reading string

unexpected behavior of while loop variable with scanf reading string


By : Andreas
Date : March 29 2020, 07:55 AM
wish helps you Judging by if(strcmp(str,"lhx")==0), you are inputting 3 characters. But str has memory for only 3 characters. To scanf it as a string, you'll need at least 4 bytes of memory for str.
You could fix by changing it to:
code :
  char str[4];
the below code the scanf is not reading my characters in the while loop?

the below code the scanf is not reading my characters in the while loop?


By : user3901886
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Whenever you get errors while running loops, try to run them manually on paper for two or three iterations which will make us understand how our code flow is happening in a loop.
In your code, when you run the loop for first time, you are taking the character into a[0], incrementing value of i and which now makes the value of i as 1, now you are comparing the value of a[1] to ' ' which is a garbage. correct it to the following code,
code :
scanf("%c",&a[i]);    
//i++;    
if(a[i++]==' ')    
   k++;    
scanf skipped after reading in integer in C, in while loop

scanf skipped after reading in integer in C, in while loop


By : Sathya Veera
Date : March 29 2020, 07:55 AM
it helps some times The problem with your code is that in case the user does not input a number your program will loop forever. This is because scanf will repeatedly try to parse the same string and keep failing. What you have to do is to match whatever the user has written and then ask again for a number:
code :
#include<stdio.h>

int main(){
    int choice;
    while(scanf("%d",&choice) <= 0){
        scanf("%*s"); // this will parse anything the user has written
        printf("Incorrect value entered, please re-enter\n");
    }
    return 0;
}
Reading dynamically characters in C with a scanf in a loop

Reading dynamically characters in C with a scanf in a loop


By : S. Vail
Date : March 29 2020, 07:55 AM
will be helpful for those in need Your loop condition while(scanf("%c", &temp) == 1 && tempOld != '\n') first reads a character and then afterwards checks if the previous character was a newline. Additionally, the value tempOld is not initialized so the behavior at the first loop iteration is actually undefined.
You need to check if the current character read was a newline, and if so, then terminate the loop. Something like:
code :
int temp;
while (i < 79 && (temp = getc(stdin)) != EOF && temp != '\n')
{
    string[i++] = temp;
}
string[i] = '\0';
Two scanf statements inside a while loop not reading properly

Two scanf statements inside a while loop not reading properly


By : Bharadwaj
Date : March 29 2020, 07:55 AM
it should still fix some issue %s is not a good idea if you want to read entire lines; it'll normally break on any whitespace character. Some dirty hacks exist to circumvent this, but I suggest you just use fgets instead:
Related Posts Related Posts :
  • N Queens Puzzle - Where is the Backtracking in this solution?
  • how to build a simple lock (mutex) on nios II cpu
  • C macro expansion of a function pointer based on for loop incrementor
  • Epoll with edge triggered and oneshot only reports once
  • how to implement (PHP Function)array_map funciton in c?
  • Find a sum of two distinct numbers from the set, closest to the query number
  • Trouble with array in function
  • C, Piping messages to child processes not working. Why?
  • CreateThread() passing struct arguments weirdly
  • How to change a char to ASCII form?
  • Pipes, dup2 and exec()
  • I can't get this mean calculated, it just displays 0 all the time
  • Expected ' ' before '=' token in struct definition
  • How to implement a MATLAB lowpass filter in C
  • Convert hexidecimal char array to u8 array in C
  • Delete function in Binary Tree in C
  • Why (int)((unsigned int)((int)v)?
  • Creating a matrix in a structure
  • C Recursion runtime error
  • C: pointer to 2d array
  • How to free a pointer in a structure array?
  • gcc -O optimization: Help me understand the effect
  • Errors on code (state machine)
  • Does node equals node on a struct initializes the rest of the struct attributes?
  • C program to copy one string into other string without using library functions
  • Macro representing a pin on a microchip
  • C code workks in Mac (Darwin 13.4), but not in Linux (2.6.32)
  • How do I read this complex C declaration?
  • why is the recursion used in finding the factorial of a number?
  • Inverting array elements (bitwise) doesn't work
  • git blame-like annotation of source files with gcov
  • Error using pthread on Windows with Mingw
  • How to create a static library with a Makefile from C source code
  • Why are there two different ways of initializing a pointer in C
  • Program runs too slowly with large input - C
  • Dangling pointer example confusion
  • Why is this program running when input is 1?
  • Malloc() doesn't work + char array clear
  • Reading debug registers on linux
  • Socket Programming - Server content written to Client (write())
  • Incrementing a string in C
  • what does a[0] = addr & 0xff?
  • unexpected EOF while looking for matching `'' while using execve()
  • What's the purpose of stack pointer alignment in the prologue of main()
  • Time from startup in linux kernel
  • UNIX socket connection refused
  • How to programmatically set IP address on Windows 7 using C
  • programming a gpu without using any library like cuda or opencl?
  • C "printf" Different output in Linux and Mac
  • Bit rearrangement/manipulation in C
  • How to find occurences of a digit with 4 in it within 50?
  • Read string separated by comma
  • how to optimize the C code, when m is too big, it can't operation
  • GCC Error Infinite While Loop
  • Row-major vs Column-major confusion
  • Parsing memory mapped file C
  • going out of bounds in array of structs in C
  • fprintf in do while loop wrote only one line in file C
  • Reading and Writing to Files in C
  • Changable amount of scanned numbers per line (scanf)
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co