I made a program to get data from a file, it works, but then the program says it stopped working.
Here is my code:
#include <stdio.h>
#include <windows.h>
#include <iostream>
using namespace std;
string show()
{
FILE *in;
char c;
in = fopen("version.txt", "r");
if(in != NULL)
{
while((c = fgetc(in)) != EOF)
{
putchar(c);
}
fclose(in);
}
else printf("Unable to open file\n");
}
int main()
{
show();
}
Your function is declared to return a
stringbut there is no return statement.This is “undefined behavior” in C++ (only in
mainyou are allowed to omit the return statement and in that case C++ will assume areturn 0;automatically – but IMO it’s nicer to also always write it).