Solved

txt files

Posted on 2011-03-11
9
292 Views
Last Modified: 2012-06-21
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


#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;
}

Open in new window

0
Comment
Question by:HelpMeMaggi
  • 4
  • 3
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 35114603
how would you want to make the choice and how would you want to use the text?
0
 

Author Comment

by:HelpMeMaggi
ID: 35114607
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.
0
 
LVL 2

Expert Comment

by:preraksheth
ID: 35114689
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!
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 2

Expert Comment

by:preraksheth
ID: 35114695
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!
0
 

Author Comment

by:HelpMeMaggi
ID: 35114717
That is very confusing. I kinda understand it but to be honest i do not understand it.
0
 

Author Comment

by:HelpMeMaggi
ID: 35114756
How do i get rid of the new line? it makes it print out on different lines.
0
 
LVL 2

Accepted Solution

by:
preraksheth earned 500 total points
ID: 35115014
See below if it helps

#include<stdio.h>

// add three lines
#define NAME 0
#define AMOUNT 1
#define SINCE 2

int main()
{
    FILE *f;
    // added var
    int state = NAME;
    char name[80];
    char amount[80];
    char since[80];
    char file_txt[1024];
    f = fopen("output.txt", "r");
   
    // assuming the first line is the first name
    while ( fgets(file_txt, sizeof(file_txt), f) != NULL )
    {
        switch(state){
            case NAME:
                 strcpy(name, file_txt);
                 state = AMOUNT;
                 break;
            case AMOUNT:
                 strcpy(amount, file_txt);
                 state = SINCE;
                 break;
            case SINCE:
                 strcpy(since, file_txt);
                 state = NAME;
                 // or whatever you want to do
                 printf("%s owes %s since %s\n", name, amount, since);
                 break;
        }

    }

    fclose(f);

    return 0;
}
0
 
LVL 2

Expert Comment

by:preraksheth
ID: 35120515
It seems there are replies provided, and I believe they are correct ones - the deletion reason is not valid
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Test against App 49 137
How to design, and implement simple interface 5 135
Digital Output does not have continuity to ground? 3 91
Using ANSI C how to Read a .csv file 10 81
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question