Thread: Smake...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could start with the FAQ. For instance:
    Work with files (C)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    2
    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.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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
    Last edited by whiteflags; 05-15-2006 at 08:59 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed