-2

I have a .txt file:

apple 123 12 1 7 3 222
danger 1234 11 223 44 87 65
what 2322 6567 54 
its 9128 88 66 12 12

EDIT: New Question. I tried using fgets to get 1 line at a time with a while function then using sscanf to go through the line and making each word/integer into its own string. So in theory first term_in should be apple then 123 then 12 and so forth. Ok now it compiles but it doesnt work. Nothing is printing out.

int offset;
char line[1000];
FILE *fp;
char term_in[1000];
fp = fopen(argv[1], "r");

while (fgets(line, sizeof(line), fp) != NULL) {
    char *data = line;
    while (sscanf(line, " %s%n", term_in, &offset) == 1) {
        data += offset;
        printf("%s", term_in);
    }
}
Diablo
  • 21
  • 6
  • 1
    This is not a free homework service – Ed Heal Oct 15 '16 at 15:17
  • 2
    Read lines with `fgets()`; analyze them with `sscanf()`. You can find many questions on SO with answers that will help you. If you still have problems, ask a new question with your code so far, explaining exactly what your current problem is. – Jonathan Leffler Oct 15 '16 at 15:37
  • 3
    I think that it is necessary that Member to hold the number of stored elements . – BLUEPIXY Oct 15 '16 at 15:40
  • @JonathanLeffler How do i use sscanf() if I dont have a format, since i dont know how many integers there will be. – Diablo Oct 15 '16 at 15:52
  • 1
    See [Using `sscanf()` in loops](http://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops/3975254?s=1|1.8880#3975254). – Jonathan Leffler Oct 15 '16 at 15:53
  • @JonathanLeffler I had a look at it but im still confused. So i did `while (fgets(line,1000,fp) == 1)` to get lines from my text. Then I tried doing `char word[100]; while (sscanf(data, " %s%n", *word , &offset) == 1) {data += offset; printf("read: %s; offset = %5d\n", word, offset); }` But its not working – Diablo Oct 15 '16 at 16:21
  • The loop condition should be `while (fgets(line, sizeof(line), fp) != NULL)` (where the change in comparison is crucial; the respelling of `1000` as `sizeof(line)` is not). Your condition means that the inner loop is never entered. I'm assuming, of course, that your transcription into a comment is accurate. Consider adding your code to the question; it would improve it, and gives people something to answer without just writing the code for you (which is not what SO is for). Read about how to create an MCVE ([MCVE]). – Jonathan Leffler Oct 15 '16 at 16:25
  • @JonathanLeffler I edited the question can you have a look at it thanks. – Diablo Oct 15 '16 at 16:38
  • 1
    `sscanf(line, " %s%n", *term_in, &offset)` --> `sscanf(data, "%s%n", term_in, &offset)`. (Also need `fp = stdin;` or `fp = fopen("filename", "r");` etc.) – BLUEPIXY Oct 15 '16 at 16:43
  • @BLUEPIXY Ok so i did that and it compiles but the code isnt working since the printf is not showing on my command line. So the loop isnt running – Diablo Oct 15 '16 at 16:47
  • Your `printf()` won't show much because you don't include a newline at the end of the output. Use `printf("[%s]\n", term_in)` or similar. The square brackets help you see the limits of the strings (not a major issue here, but a useful technique in general). – Jonathan Leffler Oct 15 '16 at 16:50
  • @JonathanLeffler I have done that but still nothing from printf() I also have some printf() after and before the loop to see if they work and they work fine. So I think theres something wrong with the sscanf loop or fgets loop. – Diablo Oct 15 '16 at 16:54
  • @Diablo Tell me your input , output and expected output. – BLUEPIXY Oct 15 '16 at 16:54
  • `sscanf(line,` --> `sscanf(data,` – BLUEPIXY Oct 15 '16 at 17:01
  • @BLUEPIXY sorry im an idiot :C. but thanks a lot for your help. – Diablo Oct 15 '16 at 17:07

1 Answers1

0

You're getting close. Here's a minor adaptation — an MCVE (Minimal, Complete, Verifiable Example) that works:

#include <stdio.h>

int main(void)
{
    int offset;
    char line[1000];
    FILE *fp = stdin;   // Important but simple change
    char term_in[1000];

    while (fgets(line, sizeof(line), fp) != NULL) {
        char *data = line;
        while (sscanf(data, "%s%n", term_in, &offset) == 1) { // Changes
            data += offset;
            printf("[%s]\n", term_in);  // Trivial change
        }
    }

    return 0;
}

Here's an example run (it was called rl59.c and compiled to rl59):

$  rl59 <<< 'Once upon a time, there was a country that held nice, civilized presidential elections.'
[Once]
[upon]
[a]
[time,]
[there]
[was]
[a]
[country]
[that]
[held]
[nice,]
[civilized]
[presidential]
[elections.]
$

And given a multi-line input file:

So she went into the garden
to cut a cabbage-leaf
to make an apple-pie
and at the same time
a great she-bear coming down the street
pops its head into the shop
What no soap
So he died
and she very imprudently married the Barber
and there were present
the Picninnies
and the Joblillies
and the Garyulies
and the great Panjandrum himself
with the little round button at top
and they all fell to playing the game of catch-as-catch-can
till the gunpowder ran out at the heels of their boots

The output was:

[So]
[she]
[went]
[into]
[the]
[garden]
[to]
[cut]
[a]
[cabbage-leaf]
[to]
[make]
[an]
[apple-pie]
[and]
[at]
[the]
[same]
[time]
[a]
[great]
[she-bear]
[coming]
[down]
[the]
[street]
[pops]
[its]
[head]
[into]
[the]
[shop]
[What]
[no]
[soap]
[So]
[he]
[died]
[and]
[she]
[very]
[imprudently]
[married]
[the]
[Barber]
[and]
[there]
[were]
[present]
[the]
[Picninnies]
[and]
[the]
[Joblillies]
[and]
[the]
[Garyulies]
[and]
[the]
[great]
[Panjandrum]
[himself]
[with]
[the]
[little]
[round]
[button]
[at]
[top]
[and]
[they]
[all]
[fell]
[to]
[playing]
[the]
[game]
[of]
[catch-as-catch-can]
[till]
[the]
[gunpowder]
[ran]
[out]
[at]
[the]
[heels]
[of]
[their]
[boots]
Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Ok so its printing but its printing the first term infinitely – Diablo Oct 15 '16 at 17:03
  • tests2 test0-ind.txt – Diablo Oct 15 '16 at 17:04
  • So you haven't made all the fixes to the `sscanf()` line, have you. You're still using `sscanf(line, …` and not `sscanf(data, …`, as BLUEPIXY pointed out in a recent comment. – Jonathan Leffler Oct 15 '16 at 17:04
  • Thats what I enter onto the commandline. test0-ind.txt is the text with all the text and tests2 is my c program. Thats why I use `fp = fopen(argv[1], "r")` – Diablo Oct 15 '16 at 17:05
  • Yes; you can do that — you have to add code to check that there was an `argv[1]` and that the file was opened correctly, and close the file if it was opened. It's a lot easier to just read standard input. But the crux is still that you have to get _all_ the changes into the `sscanf()` line. – Jonathan Leffler Oct 15 '16 at 17:06
  • Holy shit I am blind, thank you so much for pointing that out. Alright it works now. – Diablo Oct 15 '16 at 17:07