Официальный сайт студ.городка НГТУ
Программирование и БД » [C\C++] чтение из файла/смещение курсора 

#1  19.12.07 21:31

[C\C++] чтение из файла/смещение курсора

подскажите как сместить курсор на n-ую строчку в файле!

чет не могу придумать как обойтись без самописных функций, мож какие библиотечные есть?

Offline

#2  19.12.07 23:11

Re: [C\C++] чтение из файла/смещение курсора

fgets, fgetws

Get a string from a stream.

Код::

char *fgets( 
   char *str,
   int n,
   FILE *stream 
);
wchar_t *fgetws( 
   wchar_t *str,
   int n,
   FILE *stream 
);

Parameters
str
Storage location for data.

n
Maximum number of characters to read.

stream
Pointer to FILE structure.

Return Value
Each of these functions returns str. NULL is returned to indicate an error or an end-of-file condition. Use feof or ferror to determine whether an error occurred. If str or stream is a null pointer, or n is less than or equal to zero, this function invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, errno is set to EINVAL and the function returns NULL.

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.

Remarks
The fgets function reads a string from the input stream argument and stores it in str. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in str is appended with a null character. The newline character, if read, is included in the string.

fgetws is a wide-character version of fgets.

fgetws reads the wide-character argument str as a multibyte-character string or a wide-character string according to whether stream is opened in text mode or binary mode, respectively. For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.

Offline

#3  19.12.07 23:11

Re: [C\C++] чтение из файла/смещение курсора

n раз

Offline

#4  20.12.07 00:14

Re: [C\C++] чтение из файла/смещение курсора

спасиб, перевожу)

Offline

#5  20.12.07 00:43

Re: [C\C++] чтение из файла/смещение курсора

а разве fgets() не до пробела сичтывает?

Offline

#6  20.12.07 02:25

Re: [C\C++] чтение из файла/смещение курсора

как вариант

Код: cpp:

FILE * file = fopen( bla-bla-bla ) ;
char buf[200];
for ( int i=0; i<n-1; i++ )  
   fscanf( file, "%s\n", buf );
// после этого цикла файловый указатель (file) нацелен на начало n-ой строки

Offline

#7  20.12.07 10:37

Re: [C\C++] чтение из файла/смещение курсора

Kosh_Mar, нет, gets как раз читает до конца строки... а вот функции scanf("%s"... - до первого разделителя...

Offline

#8  20.12.07 14:19

Re: [C\C++] чтение из файла/смещение курсора

Andron_, и правда. Нет вчера проверить, надо было обязательно спросить :)
Fatboy, а вот твой способ, несмотря на "\n", считывает всего лишь до пробела

Offline

#9  20.12.07 15:46

Re: [C\C++] чтение из файла/смещение курсора

...

Исправлено R0mm (20.12.07 15:49)

Offline

Программирование и БД » [C\C++] чтение из файла/смещение курсора 

ФутЕр:)

© Hostel Web Group, 2002-2025.   Сообщить об ошибке

Сгенерировано за 0.788 сек.
Выполнено 14 запросов.