-1

I'm a student, I am wondering... How can I make a program that can Get some data from my text file to a variable on my program and print them

Example:

My Text File

I,Ate,Cookies
She,Drink,Coffee
Tom,Wears,Pyjamas

My code

main()
{
    FILE *fp=fileopen("c:\\textfile.txt","r");
    char name[20],action[20],item[20];
    prinf("Enter name: \n");
    scanf("%s",&name);

    /* I dont Know what to do next */
}

I though about some checking code:

if (name==nametxt)  /*nametxt is the first line on the text file */
{
    printf("%s\n %s\n %s\n",name,action,item);
}

If the name is "I",the output would look like this :

 Enter name:
 I
 I
 Eat
 Cookies

A help will satisfy my curiosity thanks in advance

Freddykong
  • 95
  • 12
Archie
  • 11
  • 2

4 Answers4

0

There is different ways to read data from a FILE * in C :

  • You read only one character : int fgetc(FILE *fp);.
  • You read a whole line : char *fgets(char *buf, int n, FILE *fp); (take care to buf, it must point to allocate memory).
  • You read a formatted string, which is your case here : int fscanf(FILE *stream, const char *format, ...), it works like printf() :

This way :

char name[20], action[20], item[20];
FILE *f = fopen("myfile.txt", "r");

if (! f)
    return;

if (3 == fscanf(f, "%19[^,\n],%19[^,\n],%19[^,\n]\n", name, action, item))
    printf("%s %s %s\n", name, action, item)

%30[^,\n], here is used to read of whole object of your line, except , or \n, which will read item by item the content of your string.

Quentin
  • 724
  • 7
  • 16
0
  • You are reading characters from file until you receive new line character (\n) or fill an array, then you return characters stored in an array passed by caller.

  • From this returned array you may get separated values with strtok.

  • Repeat until you receive 0 from getline (Getline received EOF from file.)

Here is simple example with your own getline function which you may modify.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getline(char s[],int lim, FILE * fp)
{
    int c, i;
    for (i=0; i < lim-1 && (c=fgetc(fp))!=EOF && c!='\n'; ++i)
    {
        s[i] = c;
    }
    if (c == '\n')
    {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

int main()
{
    FILE * fp = fopen("c:\\textfile.txt", "r");

    char line[100];
    char * ptr;

    while (getline(line, 100, fp))
    {
        ptr = strtok(line, ",");

        while( ptr != NULL )
        {
           printf(" %s\n", ptr);

           ptr = strtok(NULL, ",");
        }
    }

    return 0;
}

Output

I
Ate
Cookies

She
Drink
Coffee

Tom
Wears
Pyjamas

Storing strings into variable isnt tough, here is an example

strcpy(name, ptr);

But be careful, writing outside of bounds have undefined behavior.

strncpy(name, ptr, 100); You can limit number of copied characters with strncpy, but be careful, this function is error-prone.
kocica
  • 6,412
  • 2
  • 14
  • 35
0

You can do like this,

  1. Go on reading characters from a file, after every character is read compare with ',' character.
  2. If the character read is ',' then you have finished reading the name, otherwise store it in a character array and continue reading the file.
  3. Once you hit ',' character, terminate the character array with null character(Now you have a complete name with you).
  4. Compare this character array with a string you receive as input using a strcmp(String compare function). If its it matches decide what you wanna do?

I hope i am clear.

yadhu
  • 1,253
  • 14
  • 25
0

start with like this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DATA_FILE "data.txt"
#define LEN 19
#define SIZE (LEN+1)
//Stringification
#define S_(n) #n
#define S(n) S_(n)

enum { NOT_FOUND, FIND };

int pull_data(const char name[SIZE], char action[SIZE], char item[SIZE]){
    int ret = NOT_FOUND;

    FILE *fp = fopen(DATA_FILE, "r");//fileopen --> fopen
    if(fp == NULL){
        perror("fopen:");
        exit(EXIT_FAILURE);
    } else {
        char nametxt[SIZE];
        *action = *item = 0;
        while(fscanf(fp, "%" S(LEN) "[^,],%" S(LEN) "[^,],%" S(LEN) "[^\n]%*c", //"%19[^,],%19[^,],%19[^\n]%*c"
            nametxt, action, item) == 3){
            if(strcmp(name, nametxt) == 0){//Use strcmp for comparison of strings
                ret = FIND;
                break;
            }
        }
    }
    fclose(fp);
    return ret;
}

int main(void){
    char name[SIZE], action[SIZE], item[SIZE];

    printf("Enter name: \n");//prinf --> printf
    if(scanf("%" S(LEN) "s", name) == 1){
        if(pull_data(name, action, item) == FIND){
            printf("%s\n%s\n%s\n", name, action, item);
        } else {
            printf("%s not found.\n", name);
        }
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70