Hi
i am trying to write a function in C which will print a substring oout of a string in loop with a token seperator.
For example
i have a file which contains the following lines

aaaaa
bbbbb
ccccc
ddddd

i have to read each line and then copy to a variable. Once end is reached , i have to start from again

i am trying to use strtok but how to go to the starting of function and then strtok again

Below is the code snippet

#include <stdio.h>
#include<string.h>
char *result;
void new();
int main(void) {
      int c=0,i,n;
      FILE *fp;
      char str[40000];
      fp=fopen("test.txt","r");
      n=fread(str,1,40000,fp);
      printf("str value is <%s>\n",str);
      result = strtok(str,"\n");
      for(i=0;i<50;i++)
      {
        printf("result value is <%s>\n",result);
        new();
        result=strtok(NULL,"\n");
      }

    return(0);
    }
void new()
{
 char *temp;
 printf("result value in function is <%s>\n",result);
 strcpy(temp,result);
 printf("temp value is <%s>\n",temp);

}

Thanks
Vit

i have to read each line and then copy to a variable.

Perhaps strtok() may not be the best way to handle this situation (although I guess it can be used using the '\n' newline escape character as a delimeter); however, your goal seems to be to read in 1 line at a time, so I will suggest the <string> class function known as getline().

use getline() to extract single lines at a time from (in this case) your .txt file. Here is an example:

(oh yeah, since you posted in the c++ forum, you'll get ye' answer in c++)

#include<fstream>
#include<string>
...
...
...
ifstream infile;
string line_of_text, entire_document;
....
....
while(getline(infile,  line_of_text))
{
     entire_document += line_of_text;
}

So there ye' have it. The code reads in line-at-a-time and concantinates the line of text to another string object (entire_document) which will eventually hold the contents of the entire .txt file. Hope you can translate this to C.

strtok() is mostly used for parsing up individual c-string into sub c-strings.. but can also have applications with regular strings (not the preferred method, but can be done.)

Hi Clinton
First i should say thanks for the quick response.
i think i can use gets in place of the getline. But it doesnt loop on the lines

My requirement is to open a file and copy line by line from the file into string. Once the end of the file is reached, i have to start again from the starting of the file.

For example say i have three lines in my file as below

Line 1
Line 2
Line 3.

So my result variable should contain each time something like
first iteration : "Line 1"
second iteration: "Line 2"
Third iteration: "Line 3"
Fourth iteration:"Line1" // start from the first line of the file
fifth iteration:"Line2"
.
.
.
.
so on.. infinite loop


thanks
Vit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.