读书人

c语言指针的有关问题!

发布时间: 2012-11-10 10:48:51 作者: rapoo

c语言指针的问题!!
程序大概是这样的。

for(int i=0; i<lines; ++i){
36 char* CS = readLinefromFile(fname, i+1);
37 for(int j=0; j<strlen(CS); ++j)
38 printf("%c",CS[j]);
39 printf("\n");
40 printf("read line %d-----length: %d\n",i+1,strlen(CS));
41 SNRs_nav[i] = compute(CS,false);
42 SNRs_opt[i] = compute(CS,true);
43 free(CS);
44 CS=NULL;
45 }

readLinefromFile()函数是读取文件名为fname的第i+1行数据,返回char*,返回的是在该函数里malloc的指针变量。单独测试的时候没有任何问题,不过放在循环里老是出问题:不加CS=NULL,CS每次读取的行总是不对,比如第一行200个数据,第二行50个数据,读第一行是正确的,但读第二行的时候也读出来200个数据,后面的150个数据是第一行的,第三行也是如此;加上CS=NULL后,总是出现乱码。文本的内容全是字母。

对C语言不熟,还望高手指教!

char* readLinefromFile(char* fname, int i){
*****
char* cs = (char*)malloc(sizeof(char)*10000);//10000是随便定义的大数
*****
return cs;
}

我是在linux下写的代码,用的文件是在windows下处理的txt文本。

[解决办法]
前排支持,期待大牛来解决。
[解决办法]
fgets, fgetws
Get a string from a stream.

char *fgets( char *string, int n, FILE *stream );

wchar_t *fgetws( wchar_t *string, int n, FILE *stream );

Function Required Header Compatibility
fgets <stdio.h> ANSI, Win 95, Win NT
fgetws <stdio.h> or <wchar.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

Each of these functions returns string. NULL is returned to indicate an error or an end-of-file condition. Use feof or ferror to determine whether an error occurred.

Parameters

string

Storage location for data

n

Maximum number of characters to read

stream

Pointer to FILE structure

Remarks

The fgets function reads a string from the input stream argument and stores it in string. 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 string is appended with a null character. The newline character, if read, is included in the string.

fgets is similar to the gets function; however, gets replaces the newline character with NULL. fgetws is a wide-character version of fgets.

fgetws reads the wide-character argument string 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.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_fgetts fgets fgets fgetws


Example

/* FGETS.C: This program uses fgets to display
* a line from a file on the screen.
*/

#include <stdio.h>

void main( void )
{
FILE *stream;
char line[100];

if( (stream = fopen( "fgets.c", "r" )) != NULL )
{
if( fgets( line, 100, stream ) == NULL)
printf( "fgets error\n" );


else
printf( "%s", line);
fclose( stream );
}
}


Output

/* FGETS.C: This program uses fgets to display



Stream I/O Routines

See Also fputs, gets, puts

[解决办法]

C/C++ code
char* readLinefromFile(char* fname, int i){  *****  char* cs = (char*)malloc(sizeof(char)*10000);//10000是随便定义的大数  *****  return cs;} 

读书人网 >C语言

热点推荐