Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you read tab delimited strings from a txt file and put them into variables?
    primarykey
    data
    text
    <p>I have a file that I'm trying to read and fill variables with. The file consists of this: </p> <pre><code>0\ttake a nap\n 1\tstudy heap-based priority queue\n 101\treview trees for Midterm 2\n 3\tdo assignment 7\n </code></pre> <p>This may be hard to read, but you can see that there is an integer to begin with, followed by a tab, a string after that, followed by a newline. I need to take the integer and put that into a variable, detect the tab, and put the string following the tab into a variable, detect the newline, take the two variables and create a node with the information, and then start over again on the next line. After hours of scouring the internet, this is what I've come up with:</p> <pre><code>char activity[SIZE]; char position[SIZE]; char line[100]; FILE *infile; char *inname = "todo.txt"; int i = 0; infile = fopen(inname, "r"); if (!infile) { printf("Couldn't open %s for reading\n"); return 0; } while(i &lt; 100 &amp;&amp; fgets(line, sizeof(line), infile) != NULL){ sscanf(line, "%s\t%s", position, activity); printf("%s\n", position); printf("%s\n", activity); i++; } </code></pre> <p>When running this test code on the txt file above, I get this as a result:</p> <pre><code>0 take 1 study 101 review 3 do </code></pre> <p>So, it looks to me like it's getting the first number alright (as a string) and putting it into the variable, seeing the tab, and grabbing the first sequence after the tab and stopping there after putting it into the other variable. How do I rectify this situation?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload