GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Feb-2004, 23:16
etron etron is offline
New Member
 
Join Date: Feb 2004
Posts: 4
etron is on a distinguished road

read specified range of lines from file


hi everyone ...

I'm new to C ... and I am having this problem on reading a file with a specified range of lines and print those lines to the screen.

I have this file, test.txt:
this is the first line of the file
this is the second line of the file
this is the third line of the file
this is the fourth line of the file

I'm taking command line arguments for my start line no and end line no. I think I have to do some looping from the start line no to the end line no, then, I wanted to pass the current line no to a function to locate the line in the file. Then, print that line to the screen. Lastly, print "END OF FILE"

So, if the start line no is 2 and the end line is 3, the output will be:
this is the second line of the file
this is the third line of the file
END OF FILE

Thanx for the help ... :-)
  #2  
Old 12-Feb-2004, 02:05
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by etron
hi everyone ...

I'm new to C ... and I am having this problem on reading a file with a specified range of lines and print those lines to the screen.

I have this file, test.txt:
this is the first line of the file
this is the second line of the file
this is the third line of the file
this is the fourth line of the file

I'm taking command line arguments for my start line no and end line no. I think I have to do some looping from the start line no to the end line no, then, I wanted to pass the current line no to a function to locate the line in the file. Then, print that line to the screen. Lastly, print "END OF FILE"

So, if the start line no is 2 and the end line is 3, the output will be:
this is the second line of the file
this is the third line of the file
END OF FILE

Thanx for the help ... :-)

What is it you are having trouble with? What do you actually know how to do?
  #3  
Old 12-Feb-2004, 07:42
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Welcome etron.

I agree with WaltP. You should give this a shot and then post your specific problems. Reading through your program description, it should not be too hard to reduce this to a basic psuedo-code, like:
Code:
open the file loop from 0 to start line read line from file throw the line away loop from start line to end line read line from file print the line to the screen close the file print END OF FILE

Here is some file functions that may be handy. (C syntax)
CPP / C++ / C Code:
FILE* fp;                           //Define the file pointer
char string[MAX];                //This will hold the line - MAX is a constant we have defined with #define
fp = fopen("file.txt","r");       //Open a file called "file.txt" in read only mode
fgets(string,MAX,fp);           //This will read a line up to MAX size from the file into the string.
feof(fp);                           //This will check the file for the end.

Anyhow give it a shot and if you have more specific problems, let us know.
  #4  
Old 12-Feb-2004, 18:35
etron etron is offline
New Member
 
Join Date: Feb 2004
Posts: 4
etron is on a distinguished road
i'm sorry .... for the confusion ... next time ... i'll post the psuedo-code

and thank you for the idea and the code ...
  #5  
Old 12-Feb-2004, 21:27
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by etron
i'm sorry .... for the confusion ... next time ... i'll post the psuedo-code

and thank you for the idea and the code ...

Hey, no reason to be sorry. And there was no confusion. What we want to do is help you to learn programming and not just give you a quickie solution to your problem. Whats the saying, give a man a fish...

Anyway, please post back when you get some code if you are having problems and we will definitely give it the once over
  #6  
Old 17-Feb-2004, 03:49
etron etron is offline
New Member
 
Join Date: Feb 2004
Posts: 4
etron is on a distinguished road
CPP / C++ / C Code:
#define MAX_LINE_LENGTH 100

int main(int argc, char **argv[]) {
char maxline[MAX_LINE_LENGTH];
int startline, endline, result;
FILE *fp;

if (argc != 3) {
printf("Usage: Q1 FILENAME FIRST_LINE LAST_LINE.\n");
exit(1);
}
	
if ((fp = fopen(argv[1], "r")) == NULL) {
printf("Unable to open the file %s.\n", argv[1]);
exit(1);
}

if (startline > 1) {
result = find_Line(fp, startline);
if (result == -1 || result == 0) {
printerror(result);
} else {
while (startline <= endline) {
result = get_Line(fp, lineBuff);
if (result == -1 || result == 0) {
printerror(result);
}
}
}
} else {
while (startline <= endline) {
result = get_Line(fp, lineBuff);
if (result == -1 || result == 0) {
printerror(result);
}
}
}

fclose(fp);
return 0;
}

int find_Line(FILE *fp, int lineNum) {
int currentline = 0;
int stopline;
char line[MAX_LINE_LENGTH];
	
stopline = linenum - 1;
while (currentline <= stopline) {
fgets(line, MAX_LINE_LENGTH, fp);

if (ferror(fp)) {
return -1;
}

if (feof) {
return 0;
}

currentline++;
}

return 1;
}

int get_Line(FILE *fp, int lineBuff) {
}

void printerror(int no) {
if (no == -1) {
printf("Error in command execution.\n");
}

if (no == 0) {
printf("!!END OF FILE REACHED!!\n");
}
}

int find_Line(FILE *fp, int lineNum)
- fp : a FILE pointer to an open file stream to read from
- lineNum : number (single integer) of the line to find
- Function should leave the file’s read-write pointer at the start of the lineNum line from the current position. Eg., if lineNum is 1 then function should do nothing, while if 3 then it should move two lines ahead.
- should not itself print any error messages and it must always return. It
should return -1 if error occurs during its execution, 0 (zero) if the specified line does not exist (the EOF is reached first), or 1 (one) if it is successful.

int get_Line(FILE *fp, int lineBuff)
- fp : a FILE pointer to an open file stream to read from
- lineBuff : a pointer to memory in which to store the next line in the file
- Get the next line in the file starting from the current position in the file, and store it in the given memory as a legal C string. Note that the EOF character (‘/n’) is not part of the line. It denotes the end of a line, it is a separator. The read-write pointer for the file should be left at the start of the next line.
- should not itself print any error messages and it must always return. It
should return -1 if error occurs during its execution, 0 (zero) if the specified line does not exist (the EOF is reached first), or 1 (one) if it is successful.

and the Memory for the line buffer is to be allocated in main as an array.

This is my c file. Ok, what should I code inside the get_line function ... been thinking and still no code poping into my head ... and if I want to eliminate the max line assumption (dynamically allocated the memory to store the characters), what should I do? and if there is anything wrong with the coding, please correct me ... ok? thanx ...

thanx for the help ...
  #7  
Old 17-Feb-2004, 07:49
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Etron.

That is an excellent start! I don't think that you are too far away. Can I make a couple of suggestions? First of all, on the coding style. This isn't a big deal, but tabs really help in the readability of a program. I have posted a thread here that has an attached file with some comments on C style.

Second, your descriptions of your functions are great. Why not include these right in your code? I have attached a comment from one of my programs as an example. It is pretty similar to what you have done.
CPP / C++ / C Code:
/****************************************************************
*****************************************************************
*** db::sort_last                                             ***
*****************************************************************
*** Parameters:                                               ***
***    NONE                                                   ***
*****************************************************************
*** Returns:                                                  ***
***    DB_OKAY: If the sort is processed properly             ***
*****************************************************************
*** Notes:                                                    ***
***     This is a private function that is called by the      ***
***     put_data functions.  The private variable crecord     ***
***     should in all cases be pointing at the current entry. ***
***     This function only places the current item in the     ***
***     correct order, it does NOT perform a full sort.       ***
***     TODO: This function may be implemented different in   ***
***     the future to better handle larger data sets.  However***
***     this should be seamless to the rest of the program.   ***
*****************************************************************
*** History:                                                  ***
***     7/27/02: Created function (drs)                       ***
*****************************************************************
****************************************************************/
This makes for better maintenance as you have a record of what the function should be doing...

Let me look at your code a little bit and I will get back to you on your questions.
  #8  
Old 17-Feb-2004, 17:47
etron etron is offline
New Member
 
Join Date: Feb 2004
Posts: 4
etron is on a distinguished road
thanx dsmith ... u such a great help to me ...
  #9  
Old 18-Feb-2004, 08:04
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by etron
This is my c file. Ok, what should I code inside the get_line function ... been thinking and still no code poping into my head ... and if I want to eliminate the max line assumption (dynamically allocated the memory to store the characters), what should I do? and if there is anything wrong with the coding, please correct me ... ok? thanx ...

thanx for the help ...

Your getline function can be really close to your find line function. (In fact I would make them the same function, but the way you are doing it is fine..)

Your difference should be after the call to fgets:
CPP / C++ / C Code:
fgets(line, MAX_LINE_LENGTH, fp);
rem_endline(line);                            //this is a function you need to write
string = (char*) malloc(strlen(line)+1);
strcpy(string,line);

The rem_endline should simply parse through your string to find the '/n' and replace it with a NULL (0).
The other two lines dynamically allocate memory for the exact size of the string and then store it to the variable "string"

The MAXLINE is going to be necessary. You may want to even set it to a higher amount. This prevents memory overwrites because fgets is limited to the number of charecters specified. You could specify the size of MAXLINE dynamically, by asking for the maximum possible line size when the program starts. Then you would need to allocate your line dynamically as well with:
CPP / C++ / C Code:
char* line = (char*) malloc(MAXLINE);

At first glance, I don't see anything wrong with your code, except the readability with the tabs . However, I have not tried to compile this, have you? I always find stupid syntax errors when I compile no matter how small the file...
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading from file jmenendezr C++ Forum 2 26-Feb-2006 16:00
Mozilla Thunderbird dsmith Computer Software Forum - Linux 9 01-Mar-2005 11:56
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Scripts not being read... amneziac85 Apache Web Server Forum 2 26-Jan-2004 05:42
How Do i get php to find out the file type of a file for me? viperman95833 MySQL / PHP Forum 2 08-Mar-2003 09:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.