HelpMeMaggi
asked on
txt files
I want to be able to read off of a text file but not the whole thing at one time. I want to be able to choose what i want to use.
This is what i have. A loop but it prints everything out.
my text file has...
5(number of people)
Josh
300.00
1955
Alvin
300.00
1956
Chad
300.00
1966
Ben
300.00
1968
Arnold
300.00
1970
This is what i have. A loop but it prints everything out.
my text file has...
5(number of people)
Josh
300.00
1955
Alvin
300.00
1956
Chad
300.00
1966
Ben
300.00
1968
Arnold
300.00
1970
#include<stdio.h>
int main()
{
FILE *f;
char file_txt[1024];
f = fopen("output.txt", "r");
while ( fgets(file_txt, sizeof(file_txt), f) != NULL )
{
fprintf(stderr,"%s", file_txt);
}
fclose(f);
return 0;
}
how would you want to make the choice and how would you want to use the text?
ASKER
Well lets say i want to get all the names on one single line when i print them out. Or all the years on one line.
What i want to print out is "Josh owes me 300.00 since 1955."
and i want to print out the rest of the people the same.
What i want to print out is "Josh owes me 300.00 since 1955."
and i want to print out the rest of the people the same.
You should do the following (assuming you have correct formatting for your file ensured)
- create an int variable state - it can have three possible values: NAME, AMOUNT, SINCE (defined as say 0, 1, 2)
- initially set the state to NAME
- within the read loop, discard everything before first name
- now apply following logic in the loop
- check the state
- if state = NAME read line and store in local var name and set state to AMOUNT
- if state = AMOUNT read line and store in local var amount and set state to SINCE
- if state = SINCE read the line, store in local var since, print to your output file and change state to NAME
- Repeat above till you finish the file!
- create an int variable state - it can have three possible values: NAME, AMOUNT, SINCE (defined as say 0, 1, 2)
- initially set the state to NAME
- within the read loop, discard everything before first name
- now apply following logic in the loop
- check the state
- if state = NAME read line and store in local var name and set state to AMOUNT
- if state = AMOUNT read line and store in local var amount and set state to SINCE
- if state = SINCE read the line, store in local var since, print to your output file and change state to NAME
- Repeat above till you finish the file!
More clarified description
- create an int variable state - it can have three possible values: NAME, AMOUNT, SINCE (defined as say 0, 1, 2)
- initially set the state to NAME
- within the read loop, discard everything before first name
- now apply following logic in the loop
- check the state (better done using a switch/case on state var)
- if state = NAME read line and store in local var name and set state to AMOUNT
- else if state = AMOUNT read line and store in local var amount and set state to SINCE
- else if state = SINCE read the line, store in local var since, print to your output file and change state to NAME
- Repeat above till you finish the file!
- create an int variable state - it can have three possible values: NAME, AMOUNT, SINCE (defined as say 0, 1, 2)
- initially set the state to NAME
- within the read loop, discard everything before first name
- now apply following logic in the loop
- check the state (better done using a switch/case on state var)
- if state = NAME read line and store in local var name and set state to AMOUNT
- else if state = AMOUNT read line and store in local var amount and set state to SINCE
- else if state = SINCE read the line, store in local var since, print to your output file and change state to NAME
- Repeat above till you finish the file!
ASKER
That is very confusing. I kinda understand it but to be honest i do not understand it.
ASKER
How do i get rid of the new line? it makes it print out on different lines.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It seems there are replies provided, and I believe they are correct ones - the deletion reason is not valid