09-16-2005
jumping to a specific line in a text file
hi everybody!
i need to read a specific line from a text file using C. can any one suggest how to do it.
i m aware abt fread(), fwrite(), fseek()... but using these allows the pointer to be moved 1 character at a time. Is there a way i could jump directly to a line if i know the line number?
i would also like to know how to read rows of a text file and store it in a variable if the type of data in each row is different.
my sample text file looks like this:
username:xyz
login time: 00:00:00
status:0
Thanks!
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi,
I wanted to add specific text to each row in a text file containing three rows. Example:
0 8 7 6 5 5
7 8 9 0 7 9
7 8 9 0 1 2
And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this:
21 0 8 7 6 5 5
7 8... (4 Replies)
Discussion started by: hertingm
4 Replies
2. Shell Programming and Scripting
Given a text file, how do you add a line of text after a specific line number? I believe I would want to use "sed" but I am unsure of the syntax. Thank you.
Mike (5 Replies)
Discussion started by: msb65
5 Replies
3. Shell Programming and Scripting
this is utterly embarassing :(
after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :(
sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies
4. Shell Programming and Scripting
hello,
I have got the following problem that I am hoping someone can help with please.
1. I have got the following text file (below) , the columns data are
'Test Day', 'Board', 'Betting Number'.
TEXT FILE
============================================
1 3 02-01-27-28-29-30
0 1... (1 Reply)
Discussion started by: jermaine4ever
1 Replies
5. Shell Programming and Scripting
Hello,
I have a problem which is giving me headache for days, can some please help. Please see code and text fiel below. Please see text in red for the problem I am facing
# Program gets an input x from user
while read line ; do
echo... (4 Replies)
Discussion started by: jermaine4ever
4 Replies
6. Programming
Hi,
I have an output file which has more than 1,000,000,000 lines. I am accessing this file in another C++ program. Now while accessing the output file using cin, I want to jump, say, to the 5,000,000th line directly and start accessing data from there. Is this possible? Could someone please... (4 Replies)
Discussion started by: mugga
4 Replies
7. UNIX for Dummies Questions & Answers
Hi all, I have a script which uses a basic line to add text into another file e.g.
grep -i test * >> test.txt
Is there a way I can get the output of the grep to output to a specific line in the text.txt for example output above the line starting "Bottom line..." (6 Replies)
Discussion started by: JayC89
6 Replies
8. Linux
Hello! All,
Could you please tell me how to get a specific line number from a text file?
For example below,
ABC
DEF ---> Get this line number, return to an variable
GHI
My OS is Linux.
Thank you so much for your help in advance! (3 Replies)
Discussion started by: barryxian
3 Replies
9. Shell Programming and Scripting
I have a text file like this:
subject1:LecturerA:10
subject2:LecturerA:40
if I was given string in column 1 and 2 (which are subject 1 and LecturerA) , i need to update 3rd field of that line containing that given string , which is, number 10 need to be updated to 100 ,for example.
The... (6 Replies)
Discussion started by: bmtoan
6 Replies
10. Shell Programming and Scripting
Hi
This is my first post and I'm just a beginner. So please be nice to me.
I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file?
I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies
fread(3) Library Functions Manual fread(3)
NAME
fread, fread_unlocked, fwrite, fwrite_unlocked - Perform input/output operations
LIBRARY
Standard C Library (libc.so, libc.a)
SYNOPSIS
#include <stdio.h>
size_t fread(
void *pointer,
size_t size,
size_t num_items,
FILE *stream);
size_t fread_unlocked(
void *pointer,
size_t size,
size_t num_items,
FILE *stream);
size_t fwrite(
const void *pointer,
size_t size,
size_t num_items,
FILE *stream);
size_t fwrite_unlocked(
const void *pointer,
size_t size,
size_t num_items,
FILE *stream);
STANDARDS
Interfaces documented on this reference page conform to industry standards as follows:
fread(), fwrite(): XPG4, XPG4-UNIX
Refer to the standards(5) reference page for more information about industry standards and associated tags.
PARAMETERS
Points to an array. Specifies the size of the variable type of the array pointed to by the pointer parameter. Specifies the number of
items of data. Specifies the input or output stream.
DESCRIPTION
The fread() function copies num_items of data of length size from the input stream into an array beginning at the location pointed to by
the pointer parameter.
The fread() function stops copying bytes if an End-of-File or error condition is encountered while reading from the input specified by the
stream parameter, or when the number of data items specified by the num_items parameter have been copied. The function leaves the file
pointer of the stream parameter, if defined, pointing to the byte following the last byte read, if there is one. The fread() function does
not change the contents of the stream parameter.
The fwrite() function appends num_items of data of length size from the array pointed to by the pointer parameter to the output stream.
The fwrite() function stops writing bytes if an error condition is encountered on the stream, or when the number of items of data specified
by the num_items parameter have been written. The fwrite() function does not change the contents of the array pointed to by the pointer
parameter.
The fread_unlocked() and fwrite_unlocked() functions are functionally identical to the fread() and fwrite() functions, except that
fread_unlocked() and fwrite_unlocked() may be safely used only within a scope that is protected by the flockfile() and funlockfile() func-
tions used as a pair. The caller must ensure that the stream is locked before using these functions.
NOTES
If the stream parameter is invalid, the result of calling the fread(), fwrite(), fread_unlocked(), and fwrite_unlocked() functions is unde-
fined.
RETURN VALUES
Upon successful completion, the fread(), fwrite(), fread_unlocked(), and fwrite_unlocked() functions return the number of items actually
transferred. If the num_items parameter is negative or 0 (zero), no characters are transferred, and a value of 0 (zero) is returned. If a
read or write error occurs, the error indicator for the stream is set, and errno is set to indicate the error.
ERRORS
Refer to the fgetc(3) reference page for error codes returned by fread() and fread_unlocked().
Refer to the fputc(3) reference page for error codes returned by fwrite() and fwrite_unlocked().
RELATED INFORMATION
Functions: read(2), write(2), fopen(3), getc(3), gets(3), printf(3), putc(3), puts(3), scanf(3)
Standards: standards(5) delim off
fread(3)