Help with reading a file in C.
{
char **command = 0;
int length;//Used to keep track of the length of "command"
FILE *f;//The file to be iterpreted
f = fopen(argv[1],"r");
while (1)//Infinite loop that should end when the whole file has been read
{
length = 0;
while ((*command[length] != 10) && (*command[length] != 13))//Check for CrLf
{
command[length] = (char*) malloc(1);
if (command[length] == NULL)//Check for valid pointer
{
printf("Error: can not allocate memory.");
getchar();
return 0;
}
*command[length] = getc(f);
if (*command[length] == EOF)//Chech for end of file
{
return 0;
}
length++;
}
getc(f);
printf(*command);
//Interpret(command); Not finished
free(*command);
}
getchar();
return 0;
}```
It's supposed to read one line at a time and send each line to the command interpreter. When I run it, I get a message "Interpreter.exe has stopped working." I think there is an infinite loop, but I can't find it("while(1)" should end when the end of the file is reached). This is the first time I've ever used the C standard library, so be nice.
I hope you do realize that there are different variable types for a reason. It generally makes assignment of ints to chars a bad idea. Furthermore, your compiler should provide you with enough detailed information about something like this for you to be able to tell what's wrong.
Edit: Also, unless you're storing your file in the same directory as the header files you're trying to include, I suggest you include with <> instead of "". And, the argv parameter in main is supposed to be an array last I checked.
I don't know if you knew this but the getc() function only reads in one char at a time. http://www.cplusplus.com/reference/clibrary/cstdio/getc/
Here's how I would go about reading a file.
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("\nusage ./example <file-path>\n\n");
return 0;
}
FILE *example_file;
if((example_file = fopen(argv[1],"r")) == NULL)
{
printf("\nFile does not exist\n\n");
return 0;
}
char command[100];
while(fscanf(example_file, "%s", command) != EOF)
{
printf("%s\n", command);
//Interpret(command);
}
fclose(example_file);
return 0;
}```
Nice, was waiting for you to post something here, skath. Just a warning to the OP regarding it though is to escape any spaces the path might contain, else it will not work. To OP, listen to skath here if he has something to say about C, he's more knowledgeable about it than I am for sure. However, I'd like to give you a suggestion, namely that you sit down and actually read through the basics of C and learn them well before you start fucking about with things like malloc() as you clearly need a better grasp on the basics.
Tyler3791 wrote: Thanks for the help. But I got to correct you("COM"), there is not inherent difference between ints and chars other than size. Although, the same is not true for the packed integer types like float and double. Right… it's called implicit conversion and stuff like that can get problematic and not only with data loss.
#include <stdio.h>
int main()
{
char a=255;
int b=a;
printf("%u",b);
return 0;
}
Compile it, run. You might be surprised at the output.
Anyhow, could've sworn that compilers tend to give at least warnings at such direct assignment of two different types, implicit conversion or not. But, I guess I was wrong, my apologies on that note.