I don't know what I am doing wrong, I want to write the text into the scanner.source pointer
But when I print scanner.source I get 'ELF'. I don't know what is causing this.
Edit Listed below the main file and the commands I ran
main function
int main(int argc, char *argv[])
{
if (argc > 2)
{
printf("Usage Jlox Script");
return 64;
}
else if (argc == 2)
{
runFile(argv[0]);
}
else
{
runPrompt();
}
}
runFile Function
void runFile(char *path)
{
FILE *file = fopen(path, "r");
if (file == NULL)
{
printf("file: not found");
exit(65);
}
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
scanner.source = malloc(sizeof(char) * (fileSize + 1));
if(scanner.source == NULL){
printf("error occurred allocating memory");
fclose(file);
exit(64);
}
size_t readFile = fread(scanner.source, sizeof(char), fileSize, file);
fclose(file);
if (readFile != fileSize)
{
printf("file closing here");
fclose(file);
exit(65);
}
scanner.source[fileSize] = '\0';
printf("scanner source '%s'", scanner.source);
run();
if (hadError)
exit(65);
}
text.txt
1 ()
2 **
3 ()
4 {};;
script ran
gcc -g modifiedlox.c -o modifiedlox
./modifiedlox.text.txt
You wrote in comments:
in main I just call the runFile with the text file
But in fact no, that's not what the main()
function later added to the question does. It passes argv[0]
to runFile()
, and argv[0]
is normally the name of the executable itself, as given on the command line. And on your platform, that is indeed an ELF executable, as I speculated.
If you want to forward the program's first command-line argument to function runFile()
, then that would be argv[1]
.
Incidentally, this sort of thing is exactly why we ask those who pose debugging questions to present a minimal, reproducible example. Although I'm a pretty good guesser, no one could answer this question with confidence until you presented a full enough picture of what the program was really doing.