Pedro
Pedro

Reputation: 105

C programming - Print the 3rd line of a txt

Imagine that i have on a txt this:

Hello

SLB

3

1324

how can i get the 3rd line? fgets or fscanf?

and imagine on a txt this:

8;9;10;12

how can i print the numbers separeted?

Upvotes: 1

Views: 1756

Answers (4)

David Barry
David Barry

Reputation: 2638

Another attempt at the second part of the question, to split a string up into several strings you can use strchr to walk through it as mentioned by bta, and at each delimiter insert an end of string character ('\0') and split the string in place, using pointers to reference the start of each segment.

#include <stdio.h>
#include <string.h>

int main(void) {
    /* may be read from input, initialized with value for simplicity */
    char a[] = "8;9;10;12";

    char *b, *c, *d;
    /*use strchr to get the index of ';' and replace it with and end of string 
      increment b to move it to the start of the  next string */
    b = strchr(a, ';');
    *b = '\0';
    b++;

    c = strchr(b, ';');
    *c = '\0';
    c++;

    d = strchr(c, ';');
    *d = '\0';
    d++;

    printf("a: %s, b: %s, c: %s, d:%s", a, b, c, d);

}

Obviously this would be better done with an array of strings and a loop so you aren't limited to 4 strings delimited with a semicolon, but hopefully this will give you an idea.

If you want to print the 3rd line of text of an input quick and dirty you can just use 3 fgets statements.

#include <stdio.h>

int main(void) {
    char buf[256];

    fgets(buf, 256, stdin);
    fgets(buf, 256, stdin);
    fgets(buf, 256, stdin);

    printf("%s", buf);
}

Upvotes: 0

bta
bta

Reputation: 45057

For your first question, you can use fgets to read in a single line. To get the third line, all you have to do is call fgets three times. Simply ignoring the data it gives you the first two times will effectively cause you to skip the first two lines.

As for the second question, you probably want to look at using strchr to locate the semicolon characters in the string. It will give you a pointer to a semicolon, and incrementing that pointer by 1 will give you a pointer to the next number in the list. Simply repeat this procedure until strchr returns NULL to walk through the list.

Edit: By request, here are some links to documentation for fgets and strchr in Spanish. The pages are available in a handful of other languages as well, use the links in the top-left corner to switch translations.

Upvotes: 1

wenlujon
wenlujon

Reputation: 693

for your second question, assume txt file is a.txt:

  #include <stdio.h>

static void output(char *p)
{
    while (*p != ';') {
        if (*p == '\0') {
            break;
        }
            putchar(*p);
            p++;

        }
    putchar('\n');
}

int main(void)
{
    char buf[20];
    char *p;
    int cnt = 0;
    FILE *fp;

    p = buf;

    fp = fopen("a.txt", "r");
    if (!fp) {
        return -1;
    }

    fgets(buf, 20, fp);

    output(p);

    while (1) {
        if (*p == ';') {
                p++;
                output(p);

        }
        if (*p == '\0') {
            break;
        }
        p++;
    }

    return 0;
};

Upvotes: 0

Gregory Gelfond
Gregory Gelfond

Reputation: 67

From a first glance I wouldn't use either, instead I would use strtok.

For the first problem I would split the input text into "tokens" using the newline character '\n'. For the second problem you could use ; to delimit your tokens.

Upvotes: 0

Related Questions