LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-28-2005, 04:02 AM   #1
NCC-1701&NCC-1701-D
Member
 
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88

Rep: Reputation: 16
Unhappy 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!
 
Old 06-28-2005, 04:19 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
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.
 
Old 06-28-2005, 04:23 AM   #3
NCC-1701&NCC-1701-D
Member
 
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88

Original Poster
Rep: Reputation: 16
Thank you, i also use C++ and there should be a similar way in C? But because I'm a newb, any C advice?
 
Old 06-28-2005, 05:10 AM   #4
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
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.
 
Old 06-28-2005, 06:33 AM   #5
NCC-1701&NCC-1701-D
Member
 
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88

Original Poster
Rep: Reputation: 16
Thanks very much!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reading text from a file mrobertson Programming 16 06-28-2005 12:39 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM
C++ reading from text file query lrt2003 Programming 5 05-15-2004 04:25 AM
reading a text file and outputting to another. Hardw1re Programming 28 11-03-2003 08:51 AM
reading a single line of a text file davi_cabral Linux - Software 1 10-29-2003 12:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration