Thread: How to store all the input char into text include space ?

  1. #1
    Unregistered
    Guest

    How to store all the input char into text include space ?

    Eg.
    This is good ..
    but the text file only store This
    how to store all of them be'cos it was commen .......

  2. #2
    Unregistered
    Guest
    Code:
    char line[50];
    
    fgets(line, sizeof(line), stdin);

  3. #3
    Unregistered
    Guest

    Inside this cprogramming i use fgets but. the result ..very fake

    The fgets get the cahr but i printf it, it containt many space...then how ?.........i only want the char and the space between them but not after them got many space
    Eg ..i store GOOD thing
    2nd i store ddd
    but my result printed out is
    GOOD thing ddd

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    void recorddata();
    void read_c();
    void main ()
    {
    int select;
    printf("\n\n\tChose Youe Selection-:\n");
    printf("\t1)Record Data\n");
    printf("\t2)View Your Stock Report\n");
    scanf("%d",&select);
    fflush(stdin);

    switch (select)
    {
    case 1 :recorddata();
    break;
    case 2 :read_c();
    break;
    }
    }

    void recorddata()
    {
    //cost is double
    int pro,reorder,quantity;
    char des[25],depart[2],ch;
    int cost;

    FILE *out,*product,*product1;
    out=fopen("sample.txt","a+");

    product=fopen("product.txt","r");
    fscanf(product,"%d",&pro);
    fclose(product);


    do{


    pro++;
    printf("Product ID\n");
    printf("%d\n",pro);

    do{
    printf("Description\n");
    fflush(stdin);
    fgets(des, sizeof(des), stdin);
    fflush(stdin);

    }while (strlen(des)>24);
    printf("%s",des);
    //scanf("%s",des);//cannot get space
    //fflush(stdin);


    do{
    printf("Department\n");
    fflush(stdin);
    fgets(depart, sizeof(depart), stdin);
    fflush(stdin);
    //scanf("%s",depart);
    //fflush(stdin);
    }while (strlen(depart)>2);

    do{
    printf("Cost price\n");
    scanf("%d",&cost);//cost is double
    fflush(stdin);
    }while (cost>999||cost<0);//actual is 999.99

    do{
    printf("Input reorder level\n");
    scanf("%d",&reorder);
    fflush(stdin);
    }while (reorder>99||reorder<0);


    do{
    printf("Input quantity\n");
    scanf("%d",&quantity);
    fflush(stdin);
    }while (quantity>99||quantity<0);


    fprintf(out,"%d|%s|%s|%d|%d|%d\n",pro,des,depart,c ost,reorder,quantity);




    printf("Do you want to input another new record ?(y/n)");
    scanf("%c",&ch);
    fflush(stdin);
    system("cls");
    }while (ch=='y');


    product1=fopen("product.txt","w");
    fprintf(product1,"%d",pro);
    fclose(product1);


    fclose(out);
    printf("Your Stock Data has been save successfully!!");
    main();

    }

    void read_c()
    {
    int pro,reorder,quantity;
    char des[25],depart[2];
    int cost,c=-1,b=-1;
    char status[10]="";

    system("cls");
    FILE *in;
    in=fopen("sample.txt","r");
    printf("This is reorder report\n");
    printf("ID\t| Des\t|Cost\t|Reorder\t|Quantity\t|Status\t|\n");


    while(!feof(in)){
    fscanf(in,"%d|",&pro);

    do {
    c++;
    fscanf(in,"%c",&des[c]);
    }while (des[c]!='|');

    des[c++]='\0';

    do{
    b++;
    fscanf(in,"%c",&depart[b]);
    }while(depart[b]!='|');

    depart[b++]='\0';

    fscanf(in,"%d|%d|%d\n",&cost,&reorder,&quantity);


    //if (quantity<=reorder)
    //{
    if (((reorder-quantity)/quantity)*100>=50)
    {
    strcpy(status,"urgent");
    }


    printf("%d\t %s\t %d\t %d\t %d\t %s\t\n",pro,des,cost,reorder,quantity,status);


    strcpy(status,"");
    //}

    }
    fclose(in);
    main();

    }

  4. #4
    Unregistered
    Guest

    Hey sorry

    Sorry after the good thing ................ddd
    ..... = space

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm...I don't understand your program just yet, but i CAN tell you that
    1)fscanf() will stop at the first space!
    2)fgets(des, sizeof(des), stdin) should be:
    fgets(des, strlen(des), stdin);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgets to read
    strncat to append
    fputs to display

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

  7. #7
    Unregistered
    Guest

    Hey i not really understand the functions

    The fgets , ...and other how to use
    can some one give me some example or .teach me how to use ?

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Sebastiani:

    2)fgets(des, sizeof(des), stdin) should be:
    fgets(des, strlen(des), stdin);


    That is completely wrong.

    if you did this:

    des[0] = 0;

    then used your code, then nothing would be stored in the string. it should be

    fgets(des, sizeof(des), stdin);

    Then if you want to break the string up into separate words, then use the strtok() function
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM