0

I'm having a problem when trying to read 3 letter each time, I want to read line by line and in each line to read sequentially 3 letters at a time.

when read the line when finding the \n jumps to the next line...

i try this

while ((getline(&line, &lenght, file)) != -1){
   while ((strncpy(ch, line, 3)) != NULL) { 

        let = replaceletter(tab, ch);

        if (let != 0)
            printf("%c", let);
   }
}

but it does not work, just read the first 3 letters and I wanted the entire line. I really do not know how to do this, I need help, please

3 Answers3

1

I don't understand how the second while loop is supposed to work. strncpy returns s1, IE ch in this case. ch had better be a char *.

So.. how is ch ever going to == NULL, unless it already was NULL to start off with? And if ch is NULL to start off with (the initialisation of it is not shown) then it's already a segfault :)

It might make sense if this line were

while(*(strncpy(ch, line, 3)) != NULL)) {

HTH

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
  • the ch is declared as: char *ch; – user1364304 Oct 26 '12 at 03:25
  • @user1364304: If you have `char *ch;` and copy a string into it, then you are in trouble. –  Oct 26 '12 at 03:28
  • @vlad-lazarenko In the C language, the only legitimate thing to copy a string to is a char *. man strncpy tells us: ` Name strcpy, strncpy - copy a string Synopsis #include char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n);` – GreenAsJade Oct 26 '12 at 21:19
  • @GreenAsJade: You are wrong about legitimate copy destinations. But that's not the point. The point is that passing uninitialized `ch` to `strncpy` is UB. –  Oct 26 '12 at 21:40
  • @vlad-lararenko. I agree that this is the point. I was responding to your comment that if you have a char * and copy a string to it you are in trouble. In fact, you must have a char * as the destination of a string copy. As you say, the char * that you have must also be initialised :) Maybe you ommitted the word "uninitialised", maybe you meant "if you have an uninitialised char *"? – GreenAsJade Oct 26 '12 at 21:59
0

You will probably run into problems when your line is < 3 characters long. Try a better, more-robust way to specify the number of bytes that strncpy should copy, like using some sort of Min function implementation in C (more info here).

Community
  • 1
  • 1
ecbrodie
  • 11,246
  • 21
  • 71
  • 120
  • the text file I want to read is already prepared for be read 3 at a time the problem is the \n at the end of the line – user1364304 Oct 26 '12 at 03:20
0

There are few things that could have been done better in your code:

  1. The code does not check how many characters were read. Was a line too short?
  2. It is not clear why the return of strncpy is being checked for NULL. It does not return NULL.
  3. It is not clear whether you initialize line and lenght and to what values.
  4. Unfortunately, it is hard to be a psychic and guess what is replaceletter, what it does, and how tab and ch are declared.
  5. It is a mystery as for why strncpy is called in a while loop.
  6. It seems like your program does not account for a newline character being a part of the returned string.

In other words, if you want to get more or less adequate help, it is always a good idea to provide a small, complete and working example demonstrating your problem.

If I were assigned that task, I'd probably write it like this:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main(void)
{
    int  ret     = EXIT_SUCCESS;
    char    *line    = NULL;
    size_t   linecap = 0;
    ssize_t  len;

    do {
        len = getline(&line, &linecap, stdin);
        if (len == -1) {
            if (errno != 0) {
                perror("readline");
                ret = EXIT_FAILURE;
            }
            break;
        }
        if (len < 4)
            continue; /* Line is too short, try again */
        /* printf("The line is: %s", line); */
        printf("First three letters are: '%.*s'\n", 3, line);
    } while(1);

    free(line); /* It is always a good idea to cleanup after yourself */
    return ret;
}

I hope that the above snippet is self-explanatory. But if there is something that needs clarification then please don't hesitate to ask.

Good Luck!