tags:

views:

223

answers:

4

Hi...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?

A: 

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.

Gregory Gelfond
could you give me an example
Pedro
man strtok has examples
hhafez
A: 

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;
};
wenlujon
little confuse :s
Pedro
I thought the first question is about one line. like this:Hello SLB 3 1324
wenlujon
are 4 lines, i want to print the 3rd line
Pedro
it is for your second question.
wenlujon
+1  A: 

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.

bta
like : fgets(line1,10,fp); fgets(line2,10,fp); fgets(line3,10,fp); printf("3rd line is %d \n",line3);
Pedro
Use `strchr` not `strstr` for single characters.
Alex
@Pedro if you want only the 3rd line, there's no need for 3 distinct variables. Use the same one, like: `int i; for (i=0; i<3; ++i) { fgets(line, 10, fp); } printf("3rd line is %s\n", line);`
jweyrich
@jweryrich it give me <null>
Pedro
@Alex: >.< You're right, thanks for catching that
bta
@Pedro: Inside the loop, check the return value of `fgets`. If it returns NULL, an error happened (see the link above for more info).
bta
@bta what link?
Pedro
@Pedro move your mouse over the blue words :) bta is talking about a link to the `fgets` function documentation, mentioned on his answer.
jweyrich
@jweyrich line is a int or array?
Pedro
@Pedro what does `fgets` expect as first argument? The documentation explains it clearly.
jweyrich
@jweyrich *line :)...not working
Pedro
I guess you haven't read the documentation, or you didn't understand what you read. Quoting: "The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a <newline> is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte."
jweyrich
@jweyrich i'm not very good at english...and if have programing in the middle it's like chinese :(
Pedro
@Pedro: I added some links to non-English documentation to my answer (assuming you speak Spanish based on your name, correct me if I'm wrong please)
bta
A: 

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);
}
David Barry
not working, prints another lines
Pedro
Your talking about the second snippet that prints the third line? Can you be a bit more specific about how it's not working(keep in mind, you need to type in 3 lines of input, there is no prompt)? I ran a quick test and it enters the third line of input entered for me.
David Barry
@David fgets(total,10,fp); fgets(total,10,fp); fgets(total,10,fp);Is printig the 7th line, but only a few numbers that are on the middle..
Pedro
That seems very strange, I can't think of a reason why that would happen(though hopefully someone else can). Are you using the input from your original question? I can't get it to reproduce your problem. If the input you're using is different would you mind posting it to see if I can make more sense of this?
David Barry