读文件到数组问题

时间:2008-05-10 21:51:03   来源:论坛整理  作者:  编辑:chinaitzhe
请问各路大侠,我怎么才能将文件中的字母和数字读到字符型数组中?
文件中的内容是这样的:
mickey.c 2
name.txt 14
address 6

我试着用fscanf读入,但每行只能读进一个字母和数字,我读的结果是:
m2n1a6
都是第一个字符。
谢谢大家了。
帮忙写的具体点,小弟初学。
网友回复:fread(&数组名,sizeof(字符串长度),取出个数,文件指针);
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



#include <stdio.h>

#include <string.h>



int main()

{

    char buffer[50];//读文件时的行缓冲

    char str[50][20];//存入字符串

    int data[20];//存入整数

        //打开读入文件

    FILE *fp = fopen("a.in","r");

    if(fp == NULL)

    {

        printf("file open failed!\n");

        return 0;

    }

    int i,j;

    i = 0;

        //按行读取文件中的内容到buffer

    while(fgets(buffer,100,fp))

    {

        sscanf(buffer,"%s%d",str[i],&data[i]);

          i;

    }

    for(j = 0; j < i;   j)

        printf("%d: %s %d\n",j 1,str[j],data[j]);

        fclose(fp);

    return 0;

}


输出:
1: mickey.c 2
2: name.txt 14
3: address 6
网友回复:2楼正解.
用CStdioFile也行 只是麻烦了点
网友回复:顶楼上的。
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



函数名: fgetc 

功  能: 从流中读取字符 

用  法: int fgetc(FILE *stream); 

程序例: 



#include <string.h> 

#include <stdio.h> 

#include <conio.h> 



int main(void) 

{ 

   FILE *stream; 

   char string[] = "This is a test"; 

   char ch; 



   /* open a file for update */ 

   stream = fopen("DUMMY.FIL", "w "); 



   /* write a string into the file */ 

   fwrite(string, strlen(string), 1, stream); 



   /* seek to the beginning of the file */ 

   fseek(stream, 0, SEEK_SET); 



   do 

   { 

      /* read a char from the file */ 

      ch = fgetc(stream); 



      /* display the character */ 

      putch(ch); 

   } while (ch != EOF); 



   fclose(stream); 

   return 0; 

} 


C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





函数名: fgets 

功  能: 从流中读取一字符串 

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

程序例: 



#include <string.h> 

#include <stdio.h> 



int main(void) 

{ 

   FILE *stream; 

   char string[] = "This is a test"; 

   char msg[20]; 



   /* open a file for update */ 

   stream = fopen("DUMMY.FIL", "w "); 



   /* write a string into the file */ 

   fwrite(string, strlen(string), 1, stream); 



   /* seek to the start of the file */ 

   fseek(stream, 0, SEEK_SET); 



   /* read a string from the file */ 

   fgets(msg, strlen(string) 1, stream); 



   /* display the string */ 

   printf("%s", msg); 



   fclose(stream); 

   return 0; 

} 



C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



函数名: fread 

功  能: 从一个流中读数据 

用  法: int fread(void *ptr, int size, int nitems, FILE *stream); 

程序例: 



#include <string.h> 

#include <stdio.h> 



int main(void) 

{ 

   FILE *stream; 

   char msg[] = "this is a test"; 

   char buf[20]; 



   if ((stream = fopen("DUMMY.FIL", "w ")) 

       == NULL) 

   { 

      fprintf(stderr, 

              "Cannot open output file.\n"); 

      return 1; 

   } 



   /* write some data to the file */ 

   fwrite(msg, strlen(msg) 1, 1, stream); 



   /* seek to the beginning of the file */ 

   fseek(stream, SEEK_SET, 0); 



   /* read the data and display it */ 

   fread(buf, strlen(msg) 1, 1, stream); 

   printf("%s\n", buf); 



   fclose(stream); 

   return 0; 

} 



C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



函数名: fscanf 

功  能: 从一个流中执行格式化输入 

用  法: int fscanf(FILE *stream, char *format[,argument...]); 

程序例: 



#include <stdlib.h> 

#include <stdio.h> 



int main(void) 

{ 

   int i; 



   printf("Input an integer: "); 



   /* read an integer from the 

      standard input stream */ 

   if (fscanf(stdin, "%d", &i)) 

      printf("The integer read was: %i\n", 

             i); 

   else 

   { 

      fprintf(stderr, "Error reading an \ 

              integer from stdin.\n"); 

      exit(1); 

   } 

   return 0; 

} 



网友回复:谢谢各位了!
关键字:文件数组,问题,
上一篇:学习c 的迷茫

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面