Bravo
Bravo

Reputation: 1139

Is it possible to getLine() in C starting at given index?

The getline function scans the whole input line and stores it in a given char array. Assume I want to use the getline method to scan the input starting at a given index. How can I achieve this?

For example, let's say the input is: Hello, my name is John.

I want getline to store only my name is John.

    char* cmd = NULL;
    size_t size = 0;

    getline(&cmd, &size, stdin); // code to get whole input line.

Thank you

Upvotes: 1

Views: 1464

Answers (4)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

You can use fscanf to ignore a fixed number of characters by specifying the size and an asterisk, like this:

fgets(cin, "%*6[^\n]"); // Ignore the first six characters, or up to \n
getline(&cmd, &size, stdin); // Read the rest of your string

Asterisk in the format string means "read and ignore". Six specifies the number of characters to read (and ignore).

Upvotes: 1

Gopi
Gopi

Reputation: 19874

The answer would be NO.

You can't use getline() to read based on your index. You just need to pass the address of your buffer pointer and address of size variable. Once the line is read you can parse the buffer and fetch what you need based on your index.

Upvotes: 1

quetzalcoatl
quetzalcoatl

Reputation: 33546

getline has no such capability. However, it reads from the default input stream and there are many other functions that read from that stream too.

One of the most basic reading function is getc(). It reads one single character. So, to "make" the getline start reading from Nth character, use N times getc just before it.

// "skip" N characters - read N and forget them immediatelly
for(int i=0;i<N;++i)
    getc(); // remember: that's a READ, it can FAIL

getline( ... ); // remember: that's a READ, it can FAIL

Of course, that way, all those skipped characters are unrecoverable now (well, almost, but let's skip it). Also, this method does not allow you to "move backwards". You can only skip "forwards".

If you ever need to look back at any characters in the input stream, you need some kind of buffering. Read all or a part of the input into the buffer, look at the buffer as a whole (array of data, etc), cut the interesting parts out of the buffer, load new/next data into buffer, and so on. Buffers are good! :)

However, this read-one-by-one is OK sometimes too. The main plus of this way of "skipping" is that it does not use any extra memory at all. If your data-to-be-skipped is large like several kilo/mega/../bytes then it's really wasteful to pack all it into a buffer just to throw it out immediatelly.

Upvotes: 1

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

Using the fgets function you can get the whole line as input. After that Based on the position you need, you can get that.

For example,

char arr1[20],arr[]="hi this is for testing";

strcpy(arr1,arr+10); // In this you can use the cmd for that.

You will get the string what you need.

Upvotes: 1

Related Questions