Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-28-2005, 04:02 AM
|
#1
|
Member
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88
Rep:
|
Reading text problems with C
Hi all,
I have the following problem. Suppose I write an app which gets input in a text file(i.txt) some sport records for each athlete. The file is like
<numofathlete> <time(s)>
<numofathlete> <time(s)>
...etc
Now how can I write an app which thakes all this output, sorts it and writes to an another file, o.txt the 5 best athletes, beginning with the 1st.
Please help!

|
|
|
06-28-2005, 04:19 AM
|
#2
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185
Rep:
|
Hi buddy, for this you can use stdio.h or fstream.h. i am telling you the thing using fstream.h
Just include fstream.h and declare a object of fstream class for file operations.
include <fstream.h>
int main()
{
char name[10];
int time;
fstream file; // this will make a fstream object to read/write file.
file.open("c:\\i.txt","rb"); // this will open a file in rb mode (read-binary).
file>>name; // this will read a word form the file
file>>time; // this will read the time from file
file.close(); // this will close file.
// then do your operations.
file.open("c:\\o.txt","wb"); // this will open a file in wb mode (write-binary).
file<<name; // this will write the sting stored in "name" into file.
file<<" "; // this will insert space in the file after the name.
file<<time; // this will write the value of variable "time" into file.
file<<"endl"; // this will move to next line of the file.
file.close();
}
you can use bubble sort to sort the players. and to read all the records in the file put them in a loop and store the names in a 2-D array of strings. The loop will run like this.
while (!file.eof()) // will run until eof() or End of File
Hope this will help u.
|
|
|
06-28-2005, 04:23 AM
|
#3
|
Member
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88
Original Poster
Rep:
|
Thank you, i also use C++ and there should be a similar way in C? But because I'm a newb, any C advice?
|
|
|
06-28-2005, 05:10 AM
|
#4
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185
Rep:
|
Ok if you are using C, you have to do something like.
include<stdio.h>
int main()
{
char name[10];
int time;
FILE *ptr; // pointer to the file.
ptr=fopen("c:\\i.txt","rb"); // Opens the file for reading
fread(name,sizeof(name),1,ptr); // reads 10 chars from file.
fread(&time,sizeof(time),1,ptr); // reads integer from file.
fclose(ptr);
//similarly to write the file open the file in writing mode like
ptr=fopen("c:\\o.txt","wb");
fwrite(name,sizeof(name),1,ptr);
fwrite(&time,sizeof(name),1,ptr);
fclsoe(ptr);
}
to loop until end of file you can use
while(!feof(ptr))
but in this kind of filing when you store the records in the file all record must be of same length, Like in this case all names must be of 10 char, not less not more. Even if you have some name like "jhon" you will write it as "jhon " to make it 10 char long.
|
|
|
06-28-2005, 06:33 AM
|
#5
|
Member
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88
Original Poster
Rep:
|
Thanks very much!
|
|
|
All times are GMT -5. The time now is 06:14 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|