getline error

This is a discussion on getline error within the C Programming forums, part of the General Programming Boards category; can someone please please tell me why this code won't work? Code: int main(int argc, char *argv[]){ char* tester = ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    getline error

    can someone please please tell me why this code won't work?

    Code:
    int main(int argc, char *argv[]){
    
      char* tester = "makefile";
      char* tester1 = "Makefile";
    
      if (argc == 1){
        fprintf(stderr, "memake: *** No targets specified and no makefile found. Stop.\n");
        return 2;
      }
      else if (strcmp(argv[1], tester) == 0 || strcmp(argv[1], tester1) == 0){
        char* file_to_read = argv[1];
        FILE* fr = fopen(file_to_read, "w");
        if (fr == NULL){
          fprintf(stderr, "%s: trying to open file %s for read\n", strerror(errno), file_to_read);
          exit(3);
        }
    
        while(1){
          char* line = NULL;
          size_t line_length;
          int chars_read = getline(&line, &line_length, fr);
          if (chars_read == -1){
            if (feof(fr)){
              free(line);
              break;
            }
            perror("getline");
            exit(4);
          }
    
          char* pch = strtok(line, " :");
          while(pch != NULL){
            printf("%s\n", pch);
            pch = strtok(NULL, " :");
          }
    
          free(line);
        }
    
          fclose(fr);
    
      }
    
        return 0;
    
    }
    it always fails when it tries to read a file

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    trying to open file %s for read

    "w" opens for writing

    if you know that the filename is "makefile" why to read it from command line parameters?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    thanks, stupid error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 01:17 AM
  5. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21