Jump to content

Recommended Posts

Posted

Can anyone explain how fgets is meant to be used; as I understand, fgets is setup as follows (in theory):

fgets(pointer_char, max value of line, input location);

I am trying to use fgets to read a user input line by line which is then to be printed back to me. I am trying to get the same functionality as what 'getline' would enable me to do, but I don't want to use getline as it's not a standard C function.

Any help would be much appreciated,

Thanks :)

char* buffer = NULL;
char output = NULL;

fgets(buffer, 999, stdin);
printf("%s \n",buffer);

Posted

Well, you should use char buffer[999]; so that it allocates space for it. That's pretty much all I see (except you also need to define stdin as a FILE * stdin=1;). Other than that, it looks like it should work. Of course, I don't have very much experience with fgets so I can't really tell you. I usually just use either getchar(); or read();

Posted

stdin should not need declaring if you have included stdio.h. Also as Johnycake said you need to make sure 'buffer' is allocated, either by doing

 char buffer[size_here];

or you could use the malloc function.

/* Allocate 1024 bytes to buffer */
char *buffer = malloc(sizeof(char) * 1024);

if (buffer == NULL) {
    printf("malloc failed!\n");
    return 0;
}

For more infomation on fgets see the man page here.

Posted

If you're just looking for 1 line input for like a yes/no then I'd recommend using scanf instead.

Posted
If you're just looking for 1 line input for like a yes/no then I'd recommend using scanf instead.

Scanf is really easy to buffer overflow...maybe if he's just beginning he won't be that worried about that, but personally, I'd want to start out with good habits from the beginning. Sure, you can use something like:

scanf("%999s",&whatever);

However, that doesn't look as pretty as fgets(whatever,999,1);

Posted

Thanks for all your help. The reason why I am not using scanf is because I am trying to read input via line by line and I want the function to be able to return the number of characters read if possible; in which to be able to do this, I am attempting to identify the return character "\n" so that I can manipulate text input, such as setting up a command to get the program to reprint a specific line of an inserted paragraph in the output; these individual lines are not going to be of a standard size so I'm needing to use a function that can deal with large streams.

Thanks again to all :)

  • 2 weeks later...
Posted

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define CR 0x0D
#define LF 0x0A

/* Incidentally, with this setup this is also the maximum line length... */
#define BUFFERSIZE 1024

/*
 * Do whatever needs to be done with a single line read from the file.
 * The line parameter has been zero-terminated.
 */
static void process_line(char *line) {
    printf("--> %s <--\n",line);
}

/*
 * Look for the end of a line in input. If it's found, terminate the string at that point by
 * replacing the EOL character with the NULL byte and return a pointer to the position
 * where the next line begins. If no full line could be isolated in the input, return NULL.
 */
static char* cut_off_first_line(char *input) {
    char *ptr = input;
    while (*ptr != '\0') {
        switch (*ptr) {
            case CR:    *ptr++ = '\0'; /* End the line here. */
                        if (*ptr == LF) ptr++;
                        return ptr;
            case LF:    *ptr++ = '\0'; // Terminate the line here.
                        return ptr;
        }
        ptr++;
    }
    return NULL;
}

int main(void) {
    int bytesread = 0;
    int remainder = 0;
    char *line = NULL, *ptr=NULL;
    int fd = open("input_file.txt", O_RDONLY);
    char *buffer = malloc(BUFFERSIZE);

    if (fd < 0) return -1;
    if (buffer == NULL) return -2;

    while ( (bytesread = read(fd,buffer+remainder,BUFFERSIZE-1-remainder)) > 0) {
        buffer[bytesread+remainder] = '\0'; /* Make sure whatever was read ends with the null byte. */
        line = buffer;
        ptr = cut_off_first_line(line);
        while (ptr != NULL) {
            process_line(line);
            line = ptr;
            ptr = cut_off_first_line(line);
        }
        remainder = strlen(line);
        memmove(buffer, line, remainder+1);
    }
    process_line(line);
    return 0;
}

Read the data in in chunks, process lines out of these chunks and read more data once you run out of lines, making sure to keep hold of the section of line that was left over from the last chunk.

This program will simply stop reading once it reaches the end of the file, or when it encounters a line that is longer than the buffer size (by virtue of read being told to read 0 bytes, causing it to return 0, breaking the loop).

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...