Thread: Read from a text file..

  1. #1
    Unregistered
    Guest

    Question Read from a text file..

    Compiler - Turbo c++ v4.5 (Borland)
    OS - Win Me/98
    Well i have created a program to write to a.txt file
    here's the part of the codes:

    Code:
    add()
    {
    	int x;
    	ptr=fopen("data.txt","a+");
    	for(x=0;x<2;x++)
    	{
    		printf("\nID: ");
    		scanf("%d",&field[x].id);
    		printf("\nName: ");
    		scanf(" %[^\n]",field[x].name);
    		printf("\nQty: ") ;
    		scanf("%d",&field[x].qty);
    		printf("\nPrice: ") ;
    		scanf(" %lf",&field[x].price);
    
    		fprintf(ptr, "%d|", field[x].id);
    		fprintf(ptr, "%s|", field[x].name);
    		fprintf(ptr, "%d|", field[x].qty);
    		fprintf(ptr, "%.2lf\n", field[x].price);
    	}
    	fclose(ptr);
    	return 0;
    }
    File contents stored like below
    ID|toyname|qty|price

    Ex.:
    001|mickey mouse|12|25.00
    002|donald duck|21|23.10
    003|abc,..|17|5.00

    1) How do i read line by line and display it on the screen without the "|"?
    Ex. ID : 001
    Toy: mickey mouse
    Qty: 12
    Price : 25.00

    ID : 002
    Toy: donald duck
    Qty: 21
    Price : 23.10

    Please provide me a simple program code which can read the data..

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use fscanf.

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

  3. #3
    Unregistered
    Guest
    The fscanf will not work correctly on the "mickey mouse" field. It will copy "mickey" into the variable (it stops copying when it reaches a white space character).

    Long winded, but try this.


    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"

    static void ParseFunction(char *FileBuf, int *int1, char *str1, int *int2, float *float1);
    static void CopyStrField(char *FileBuf, char *str1, int *x);
    static void CopyIntField(char *FileBuf, int *int1, int *x);
    static void CopyFloatField(char *FileBuf, float *float1, int *x);

    #define PARSE_CHAR '|'

    int main(int argc, char* argv[])
    {
    char FileBuf[31];
    int int1;
    int int2;
    char str1[31];
    float float1;

    /* fill FileBuf with some test data */
    strcpy(FileBuf, "001|mickey mouse|12|25.00");

    /* this function will parse the individual fields */
    ParseFunction(FileBuf, &int1, str1, &int2, &float1);

    /* display the results */
    printf("int1: (%03d)\n", int1);
    printf("str1: (%s)\n", str1);
    printf("int2: (%d)\n", int2);
    printf("float1: (%1.2f)\n", float1);

    return 0;
    }

    static void
    ParseFunction(char *FileBuf, int *int1, char *str1, int *int2, float *float1)
    {
    /* if you need to parse more fields, change this value
    pass more variables in */
    #define FIELD_COUNT 4
    //sscanf(FileBuf, "%d|%s|%d|%f", int1, str1, int2, float1);
    int i;
    int x = 0;

    for (i = 1 ; i <= FIELD_COUNT ; i++)
    {
    switch (i)
    {
    case 1:
    CopyIntField(FileBuf, int1, &x);
    break;

    case 2:
    CopyStrField(FileBuf, str1, &x);
    break;

    case 3:
    CopyIntField(FileBuf, int2, &x);
    break;

    case 4:
    CopyFloatField(FileBuf, float1, &x);
    break;

    default:
    printf("Invalid field number\n");
    break;
    }
    }
    }

    static void
    CopyStrField(char *FileBuf, char *str1, int *x)
    {
    int i = 0;

    while (FileBuf[*x] != '\0' && FileBuf[*x] != PARSE_CHAR)
    {
    str1[i] = FileBuf[*x];
    (*x)++;
    i++;
    }
    (*x)++;

    str1[i] = '\0';
    }

    static void
    CopyIntField(char *FileBuf, int *int1, int *x)
    {
    int i = 0;
    char TmpStr[10];

    while (FileBuf[*x] != '\0' && FileBuf[*x] != PARSE_CHAR)
    {
    TmpStr[i] = FileBuf[*x];
    (*x)++;
    i++;
    }
    TmpStr[i] = '\0';

    (*x)++;

    *int1 = atoi(TmpStr);
    }

    static void
    CopyFloatField(char *FileBuf, float *float1, int *x)
    {
    int i = 0;
    char TmpStr[10];

    while (FileBuf[*x] != '\0' && FileBuf[*x] != PARSE_CHAR)
    {
    TmpStr[i] = FileBuf[*x];
    (*x)++;
    i++;
    }
    TmpStr[i] = '\0';

    (*x)++;

    *float1 = atof(TmpStr);
    }

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    I recommend have your data in the text file like

    001
    Mickey Mouse
    12
    25
    002
    etc...

    then you can use to read the data like this

    Code:
    fscanf(fptr,"%d\n",&item);
    fgets(product, 150, fptr);  //Gets on or more words depending on characters
    fscanf("%d\n %f\n",&qty, &price);
    Or something similar

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The fscanf will not work correctly on the "mickey mouse" field. It will copy "mickey" into the variable (it stops copying when it reaches a white space character).
    Yes it will. You just have to know how to use it.

    fscanf( fp, "%[^|]", mybuffer );

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

  6. #6
    Unregistered
    Guest
    thanks! for the code anonymous!

    kwigibo: i also want to use that way but the instruction given is like that..

    btw quzah if i use fscanf .. what while condition should i use?

    how to make it read line by line till the end of file?
    could u provide a sample code...

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Line by line? fgets will read whole lines.

    For a record like this:

    001|mickey mouse|12|25.00

    fscanf( fp, "%d|%[^|]%d|%f", &number, buffer, &number2, &floatynumber );

    I believe that's correct.

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

  8. #8
    Unregistered
    Guest
    1) quzah it seems that it does't work after the name

    Code:
    	struct details temp;
    	fscanf( ptr, "%d|%[^|]%d|%f", &temp.id, temp.name, &temp.qty,
    		&temp.price);
    	printf("%d\n", temp.id);
    	printf("%s\n", temp.name);
    	printf("%d\n", temp.qty);
    	printf("%.2f\n", temp.price);
    it prints out the id and the name correctly but the rest not..

    this is the content inside txt
    1|1 tg|1|1.00

    result:
    1
    1 tg
    8823
    0.00


    2) is this while loop works? If i wanto read all the contents of txt n print it all out on screen
    Code:
    while (!feof(ptr))
    {
        fscanf(......
        printf("%d", temp.id);
    };

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Replace this:

    fscanf( fp, "%d|%[^|]%d|%f", &number, buffer, &number2, &floatynumber );

    With this:

    fscanf( fp, "%d|%[^|]|%d|%f", &number, buffer, &number2, &floatynumber );

    Works like a champ.

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

  10. #10
    Unregistered
    Guest
    yes! it works greaT!

    thanks quzah!

  11. #11
    Unregistered
    Guest
    Hi! again!


    Code:
    add()
    {
    	int x;
    	ptr=fopen("data.txt","a+");
    	for(x=0;x<2;x++)
    	{
    		printf("\nID: ");
    		scanf("%d",&field[x].id);
    		printf("\nName: ");
                                    //Here's the code changed..
                                    fgets(field[x].name, 25,stdin );
                                    fflush(stdin);
                           //      scanf(" %[^\n]",field[x].name);
    		printf("\nQty: ") ;
    		scanf("%d",&field[x].qty);
    		printf("\nPrice: ") ;
    		scanf(" %lf",&field[x].price);
    
    		fprintf(ptr, "%d|", field[x].id);
    		fprintf(ptr, "%s|", field[x].name);
    		fprintf(ptr, "%d|", field[x].qty);
    		fprintf(ptr, "%.2lf\n", field[x].price);
    	}
    	fclose(ptr);
    	return 0;
    }
    I just changed my code for the Name Input.. i want it to only accept 25 chars only.

    the prob is when it stored into text file :

    ex:

    1|mickey
    mouse|12|25.00
    2|donald
    duck|21|23.10

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. [HELP] Read text file in vc++ 2005
    By sonnb in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2007, 10:01 AM
  3. How to read chars from a text file and use them in TChart
    By Bachatero in forum C++ Programming
    Replies: 1
    Last Post: 08-29-2006, 04:03 PM
  4. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:21 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM