loop in the same string using strtok

This is a discussion on loop in the same string using strtok within the C Programming forums, part of the General Programming Boards category; Hi i am trying to write a function in C which will print a substring oout of a string in ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    loop in the same string using strtok

    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
    Code:
    #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);
    
    }
    Any other solution will also do

    Thanks
    Vit

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void new()
    {
    char *temp;
    printf("result value in function is <%s>\n",result);
    strcpy(temp,result);
    printf("temp value is <%s>\n",temp);
    What value does temp have? Why do you think it has that value?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Code:
    n=fread(str,1,40000,fp);
    printf("str value is <%s>\n",str);
    In C, strings are terminated by a null ('\0') character. Chances are that you are not getting a null character in str from the fread() call, so your printf() call is going to crash. Try this instead:
    Code:
    n=fread(str,1,sizeof(str)-1,fp);
    str[n] = '\0';
    printf("str value is <%s>\n",str);
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Hi Quzah, Bitub
    First i should say thanks for the quick response.
    Secondly sorry for the confused description.

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


    thanks
    Vit
    Last edited by vitvar; 10-26-2009 at 06:34 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need just a simple
    Code:
    rewind(fileptrName); 
    to reset the file pointer back to the start of the file. That's no problem.

    What I don't understand is WHAT you need to do with the data, and WHEN do you need to end this re-starting from the beginning of the file.

    I gather that you know these answers. They are central to your program, of course.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Basically
    i need to be continued until my process is up. Its like each time i get a request from a running process, i will create a dummy response taken from the file. The response should be in order each time.
    i.e for the first request, i will send first response from the file
    for the second request,second response.
    if its the end of responses then for the next request, i will take the first response again.

    EX: if there are 3 responses in the file

    response 1
    response 2
    response 3

    for request1 i will respond with response 1
    ..
    request 3, i will respond with response 3
    request 4 , i will respond with response 1
    so on...

    Well, i dint know why it dint struck me, but now i thought of something like this

    Copy all the contents of file into a buffer
    Split all the buffer contents whenever a new line occurs and copy into a 2 dimensional array and loop on the 2 dimensional array.. i will try to do this and see..


    thanks
    Vit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  2. Loop Optimization & String Comparison.
    By Lithorien in forum C++ Programming
    Replies: 8
    Last Post: 08-09-2004, 07:00 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21