Simple program: reads a name and a surname (John Smith) from a .txt file via fscanf, adds spaces, prints the name in the console (just as it's written in the .txt).
If compiled and ran on Win10 via
Microsoft (R) C/C++ Optimizing Compiler Version 19.14.26433 for x86
the following code does not produce the same output for the same input across different .exe launches (no recompiling). For each input it seems to have multiple outputs avaialble, between which the program decides at random.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char input_file_name[255];
FILE * input_file;
char name[255];
input_file = fopen ("a.txt","r");
do
{
if (strlen(name) != 0 )
name[strlen(name)] = ' ';
fscanf (input_file, "%s", name + strlen(name) * sizeof(char));
}while(!feof(input_file));
fclose (input_file);
printf("Name:%s\n", name);
system("pause");
return 0;
}
I will list a couple of inputs and outputs for them. As not all characters are printable, I will type type them as \ascii_code instead, such as \97 = a. The most common anomalies are \31 (Unit Separator) added at the very front of the string and \12 (NP form feed, new page) or \17 (device control 1) right before the surname (after the first space).
For "John Smith":
- "John Smith" (proper output)
- "\31 John Smith"
For "Atoroco Coco"
- "Atoroco \12Coco"
- "\31 Atoroco \16Coco"
For "Mickey Mouse"
- "Mickey Mouse" (proper)
- "\31 Mickey\81Mouse" (There is a \32 (space) in the string right before the \81, but the console doesn't show the space?!)
If compiled a different machine (MacOS, compiler unknown) it seems to work properly each time, that is it prints simply the .txt's contents.
Why are there multiple outputs produced, seemingly at random? Why are these characters (\31, \12 etc) in particular added, and no other?