-
Smake...
I have a programming assignment due pretty soon today, but have no idea how to start it. This program is like make, but simpler. It reads in a file called Smakefile and runs what is neccessary. I am having problems starting the program, specially opening file and reading the lines to get the informations I need, I can give the link to the assignment if someone is willing to help. Please PM me or reply. Thank you in advance.
-
You could start with the FAQ. For instance:
Work with files (C)
Quzah.
-
Thank, quzah. I got the file to open. Now I tried using fgets() to read a line and tried to tokenize it by using strtok(). But for some reason it's tokenizing the lines by 4 characters only. I don't know if this is allowed, but this is what I have so far,
Code:
char* line;
int main(int argc, char **argv)
{
char* file = "Smakefile";
FILE* fp = fopen(file, "rb");
line = (char*) malloc(sizeof(100));
char* token;
while(!feof(fp))
{
line = fgets(line, sizeof(line), fp);
token = strtok(line, ":");
printf("%s %d\n", token, sizeof(line));
}
}
When i run this all the tokens are broken up in 4 bytes. Oh this is the file that it's reading.
Code:
main : main.o other.o echo
gcc -o main main.o other.o
echo "Done!";
Thank you in advance again.
-
There's a FAQ on using feof to control looping. Try perhaps the return value of fgets as your loop terminator instead. Also, sizeof( line ) is going to give you the size of the pointer. Perhaps you want strlen?
Quzah.
-
Another problem here is that you took the sizeof 100 which is an integer, and an integer is 4 bytes.
Code:
line = malloc( 100 ); /* 100 bytes of memory */
But why not just declare a char array? I don't see how malloc is necessary.
FAQ: Casting malloc
-
> I have a programming assignment due pretty soon today, but have no idea how to start it
Lemme guess, you've been sitting on your ass for the past week and now you're looking for some "hail mary" pass into the end zone to rescue you.