Skip to content
Advertisement

scanf does not work in batch mode programming

I am facing issue in my code it works perfectly fine when I try to execute in linux terminal in interactive mode shown here enter image description here

but when trying to do in batch mode I am getting an unexpected behaviour from program I do not know exactly is it the address like enter image description here

mydata.txt contains 7 2 as values for and b respectively

#include<stdio.h>

int main(void){
    int a, b;
    printf("Enter Value of a, b ");
    scanf("%d %d", &a , &b);

  
    int temp = a;
    a = b;
    b = temp;
  
    printf("nAfter Swapping: a = %d, b = %dn", a, b);
    return (0);

}

Advertisement

Answer

I had this problem when using a UTF-8 file with byte order mark (BOM):

$ cat mydata.txt 
7 2
$ hexdump mydata.txt 
0000000 2037 0a32                              
0000004
$ ./a.out < mydata.txt 
Enter Value of a, b 
After Swapping: a = 2, b = 7
$ uconv -f utf-8 -t utf-8 --add-signature -o mydata_sig.txt mydata.txt
$ cat mydata_sig.txt
7 2
$ hexdump mydata_sig.txt 
0000000 bbef 37bf 3220 000a                    
0000007
$ ./a.out < mydata_sig.txt
Enter Value of a, b 
After Swapping: a = 32765, b = 0

I think it would be good for you to do a hex dump on your input file to see if there are any strange characters in it.

Advertisement