Thread: help with writing functions

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    5

    Smile help with writing functions

    hi,
    I need to write a function that prints "n" lines from a file (if n is bigger than the number of lines in the file than it just prints the whole file). I tried to write it but it doesn't work
    Code:
    void print_k_lines(char input_file[],int k)
    {
    char line[200];
    int i =0, j =0, len;
    FILE*file = fopen(input_file,"r");
    if(file == NULL)
    {
        printf("fopen failed in print_k_lines\n");
        return;
    }
    
    for(j =0; j <= k; j++)
    {
        len = strlen(line);
        while(fgets(line, len, file)!= NULL)
        {
            printf("%s\n", line);
        }
    }
    
    fclose(file);
    }

    I also need to write a function that reads a file and writes n lines on another file(same as above but the lines printed on another file).
    this what i have done so far:
    Code:
    
    
    Code:
    void copy_k_lines(char input_file[], char output_file[], int k)
    {
        int j, len;
        char line[200];
        FILE*in_file,*out_file;
        in_file = fopen(input_file, "r");
        out_file = fopen(output_file, "w");
        if (in_file == NULL || out_file == NULL)
        {
            printf("fopen failed in print_k_lines\n");
            return;
        }
        for (j = 0; j <= k; j++)
        {
            len = sizeof(line);
            while (fgets(line, len, in_file) != NULL)
            {
                fputs = (line, out_file);
            }
        }
        fclose(in_file);
        fclose(out_file);
    }


    it just doesn't work
    pls help

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well, I mean, there are lots of reasons this might not work. One of the realities of the world is that people really don't think about line length when writing files, so lines can get very long, and at a minimum, to be successful at this you should look for and find '\n' in the input before you count a line. This is because fgets() does not necessarily return complete lines:
    Quote Originally Posted by man 3 fgets
    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.
    If a line is longer than size - 1 characters, then fgets() just returns the chunk of the line.

    While you can certainly work around this limitation, it might be easiest to code this if you read files with fgetc() instead.

    Code:
    fputs = (line, out_file);
    This should be a syntax error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with writing functions using arrays
    By chrisjg04 in forum C Programming
    Replies: 6
    Last Post: 03-18-2010, 07:59 PM
  2. writing the pseudocode........functions
    By bajan in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2005, 12:59 PM
  3. Writing ADT Functions
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 04-11-2005, 05:02 PM
  4. writing functions
    By jlmac2001 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2003, 12:06 AM
  5. Writing your own functions
    By drdodirty2002 in forum C Programming
    Replies: 9
    Last Post: 04-04-2003, 11:25 AM

Tags for this Thread