![]() |
|
#1
|
|||
|
|||
read specified range of lines from filehi 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
|
||||
|
||||
|
Quote:
What is it you are having trouble with? What do you actually know how to do? |
|
#3
|
||||
|
||||
|
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:
Here is some file functions that may be handy. (C syntax) CPP / C++ / C Code:
Anyhow give it a shot and if you have more specific problems, let us know. |
|
#4
|
|||
|
|||
|
i'm sorry .... for the confusion ... next time ... i'll post the psuedo-code
and thank you for the idea and the code ... |
|
#5
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
CPP / C++ / C Code:
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
|
||||
|
||||
|
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:
Let me look at your code a little bit and I will get back to you on your questions. |
|
#8
|
|||
|
|||
|
thanx dsmith ... u such a great help to me ...
![]() |
|
#9
|
||||
|
||||
|
Quote:
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:
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:
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 GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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