比如有一个文件内容是这样的:1,23,45,6,a,
2,12,47,9,B,
只能输出其中的数字,遇到非数字则进行处理,代码如下:帮忙改下,谢谢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#define SIZE 5
struct d_list
{
int data;
struct d_list *prior;
struct d_list *next;
}dlist[SIZE];
int Get_File_Content( char *filename );
int spit( unsigned char *buf );
int write_buf( char *filename, unsigned char *buf, int len );
//读文件(读内容、解析内容)
int Get_File_Content( char *filename )
{
FILE *fp;
char buf[8192];
if( filename == NULL )
return -1;
if( ( fp = fopen( filename, "r" ) ) == NULL )
return -1;
while( 1 )
{
memset( buf, 0x00, sizeof(buf) );
fgets( buf, sizeof(buf), fp );
if( feof(fp) )
break;
//fwrite( "buf = %s",sizeof(buf),1,fp );
printf( "buf = %s", buf );
spit( buf );
}
fclose(fp);
return 0;
}
//建立双向循环链表
void create_d_list ( struct d_list **headp, int *p )
{
struct d_list *head = NULL,*tail;
if( p[0] == 0 )
*headp = NULL;
else
{
head = ( struct d_list * ) malloc ( sizeof( struct d_list ) );
head->data = *p++;
tail = head;
while( *p )
{
tail->next = ( struct d_list * ) malloc ( sizeof ( struct d_list ) );
tail->next->prior = tail;
tail = tail->next;
tail->data = *p++;
}
tail->next = head;
head->prior = tail;
}
*headp = head;
printf( "%s %d\n", __FILE__, __LINE__ );
}
//双向循环链表降序排列
void sort_d_list( struct d_list **headp1 )
{
struct d_list *p, *q;
int i,j,k,n = 0;
p = *headp1;
while( p != (*headp1)->prior )
{
n++;
p = p->next;
}
for( i = 0, p = *headp1; i < n; p = p->next, i++ )
for( j = i, q = p->next; j < n; q = q->next, j++ )
if( (p->data) < (q->data) )
{
k = q->data;
q->data = p->data;
p->data = k;
}
}
//遍历双向循环链表 写文件
void disp_d_list ( struct d_list *p1 )
{
struct d_list *p = p1;
char buffer[50];
int i = 1;
FILE *stream;
if( ( stream =fopen("file1","w+") ) == NULL )
{
printf( "cannot open the file\n" );
return;
}
do{ memset( buffer, 0x00, sizeof(buffer) );
sprintf( buffer, "%-8d", p->data );
fwrite( buffer, strlen(buffer), 1, stream );
/* if( (i % 4) == 0 )
{
printf("i = %d\n", i);
}*/
if( ((i % 6) == 0) )
fwrite( "\n", 1, 1, stream );
i++;
//printf( "buf = %8s ", buffer );
p = p->next;
}while( p != p1 );
//while( p->next != p1 );
write_buf( stream, buffer, strlen(buffer) );
fclose( stream );
return;
}
//写文件
int write_buf( char *filename, unsigned char *buf, int len )
{
int ret;
char string[] = "This is a string";
char msg[20];
FILE *fp;
if( filename == NULL || buf == NULL )
return -1;
if((fp = fopen("filename","w+")) == NULL )
{
printf( "cannot open the file\n" );
return -2;
}
ret = fwrite( dlist, sizeof( struct d_list ), 1, fp );
if( ret != 1 )
printf( "file write error\n" );
//printf( "%s %d\n", __FILE__, __LINE__ );
fread( string, strlen(string), 1, fp );
fseek( fp, 0, SEEK_SET );
fgets( msg, strlen(string)+1, fp );
fputs( "This is a test\n", fp );
fprintf( fp, "%s\n", msg );
printf( "%s\n", msg );
fclose( fp );
return ret;
}
//解析内容
int spit( unsigned char *buf )
{
//char string[] = "This is a string";
char *p,*q = ",";
p = strtok( buf, q );
while( p != NULL )
{
printf( "%s\n", p );
p = strtok( NULL, q );
}
return 0;
}
int main( int argc, char *argv[] )
{
struct d_list *head;
//int a[] = {100,5,3,99,2,7,6,9,8,10,11,70,14,13,12,16,999,1000,0};
Get_File_Content( "num" );
create_d_list(&head,"num" );
sort_d_list( &head );
disp_d_list( head );
return 0;
}