0

I need to take words from a text file and do certain tasks for specific words. But ı can't take word as variables

#include <stdio.h>
#include <stdlib.h>
int main( )
{

char * filename = "file.txt";
FILE * fp = fopen(filename, "r");
if (fp == NULL) return 1;
char c;
int count = 0;
while((c = fgetc(fp)) != EOF)
{
    if(c == ' ')
    {
        printf("\n");

    }
    else
    {
        printf("%c", c);

    }
}
fclose(fp);

return 0;

} this is what ı used for seperating words but ı can't use words to do something or call a function for example Add name "xxxx" number "xxxxxxxx" when word Add comes it takes name as "xxxx" then number so it creates a virtual file in program with name and add number in it any help ?

Herion
  • 35
  • 5
  • 1
    If you're looking to go back and work with the words read in you could read them into a char** (string) array – Patrick Mar 18 '19 at 12:21
  • Alternatively, fscanf can make it easier than working with fgetc, fscanf("%s ", stringname) will tell the program to expect a string followed by a space, which in this case would give you the first word of your input – Patrick Mar 18 '19 at 12:31
  • 2
    [`int c;`!!!](https://stackoverflow.com/q/35356322/918959) – Antti Haapala -- Слава Україні Mar 18 '19 at 13:21
  • 1
    Provide an example input and the expected output. – Costantino Grana Mar 18 '19 at 13:50
  • the function: `fgetc()` returns an `int`, not a `char` (and EOF is an `int`) – user3629249 Mar 18 '19 at 21:57
  • OT: regarding: `if (fp == NULL) return 1;` 1) please follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* 2) The code should tell the user when a problem occurs, especially a problem that causes the code to exit. Such error message should be output to `stderr`. When the error is from a C library function should also output (to `stderr`) the text reason the system thinks the error occurred. The function: `perror()` properly does all that. – user3629249 Mar 18 '19 at 22:01
  • ı change c to int but problem is still there ı need to look each line if it has keywords and ı need to take string after that keyword but ı can not think a way to do it – Herion Mar 19 '19 at 11:38

0 Answers0