Eg.
This is good ..
but the text file only store This
how to store all of them be'cos it was commen .......
Eg.
This is good ..
but the text file only store This
how to store all of them be'cos it was commen .......
Code:char line[50]; fgets(line, sizeof(line), stdin);
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();
}
Sorry after the good thing ................ddd
..... = space
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; }
The fgets , ...and other how to use
can some one give me some example or .teach me how to use ?
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.