❶ c语言中读取txt文件内容
如果下面的代码是你的话,链表的长度返回1是正常的,因为你的链表项数据只有一个字符呀!
❷ c语言 怎么从文件中读取指定内容
哥哥我想出的办法是这样的:
因为兄弟文件格式是一行一行的, 且每行开头是一个关键字,然后后面是相应数据, 所以哥哥是这么想的, 比如要读"AB2345"这个关键字对应行的内容, 那首先用变量或者宏定义定义下来, 然后从文件开头开始, 一个字符一个字符的扫描, 对每一行的开始的6个字符组成的关键字读出来跟"AB2345"这个关键字比较, 判断是否是想要读取的, 如果是, 那么通过ftell, fseek 两个函数分别得出当前指针的位置和适当移动指针的位置, 最后读取相应内容输出来!
详细代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 1024
#define KEY "AB2345"
#define KEY_LEN 7
int main()
{
 int ch = 0;
 int first = 1;//开始时的标志,因为是一个字符一个字符的扫描
 int flag = 0;//文件开头是不是所要读内容的标志
 int count = 0;//遇到'\n'的个数
 int pre_pos = 0, cur_pos = 0;//前一次和当前文件指针的位置
 char buf[BUF_SIZE] = {0};
 FILE *fp = NULL;
 fp = fopen("test.txt", "r");
 if (fp == NULL)
 {
  printf("Cann't open the file!\n");
  exit(1);
 }
 else
 {
  while ((ch = fgetc(fp)) != EOF)
  {   
   if (first)
   {
    //若要读取的内容在文件开头就有时
    //移动指针到文件开头
    fseek(fp, -1L, SEEK_CUR);
    fgets(buf, KEY_LEN, fp);
    if (strcmp(buf, KEY) == 0)
    {
     first = 0;
     flag = 1;
     continue;
    }
    else
    {
     first = 0;
    }
   }
   if (ch == '\n')
   {
    count++;//遇到'\n'的个数 
    pre_pos = cur_pos;//上次遇到'\n'时文件指针的位置
    cur_pos = ftell(fp);//当前遇到'\n'时文件指针的位置
    
    //文件开头内容符合要求的就适当移动指针位置
    //然后读取输出来
    if (count == 1 && flag == 1)
    {
     fseek(fp, 0L, SEEK_SET);
     memset(buf, 0, sizeof(buf));
     fgets(buf, cur_pos - 1, fp);
     printf("%s\n", buf);
    }
    //之后内容符合要求的就适当移动指针位置
    //然后读取输出来
    else
    {
     memset(buf, 0, sizeof(buf));
     fgets(buf, KEY_LEN, fp);     
     if (strcmp(buf, KEY) == 0)
     {
      fseek(fp, (-1) * (KEY_LEN - 1), SEEK_CUR);
      memset(buf, 0, sizeof(buf));
      fgets(buf, cur_pos-1-pre_pos, fp);
      printf("%s\n", buf);
     }
    }
   }
  }
 }
 fclose(fp);
 return 0;
}
❸ 怎么用C语言读取 TXT文件中的字符串
1、首先我们打开电脑里的VS软件,使用VS新建空工程,直接点击确定。

❹ C语言读取txt文件内容
#include<stdio.h>
#include<stdlib.h>
intmain()
{
FILE*file;
char*data;
intfileSize;
//打开文件“D:a.txt”
file=fopen("D:\a.txt","r");
//获得文件大小
fseek(file,0,SEEK_END);
fileSize=ftell(file);
fseek(file,0,SEEK_SET);
//分配内存
data=(char*)malloc(fileSize+1);
//读取文件
fread(data,sizeof(char),fileSize,file);
data[fileSize]=0;
//输出内容(你想对内容干什么都可以了)
printf("%s",data);
return0;
}
❺ C语言如何读取txt文本里面的内容
C语言可以使用fopen()函数读取txt文本里。
示例:
#include <stdio.h>
FILE *stream, *stream2;
void main( void )
{
int numclosed;
/* Open for read (will fail if file "data" does not exist) */
if( (stream = fopen( "data", "r" )) == NULL )
printf( "The file 'data' was not opened " );
else
printf( "The file 'data' was opened " );
/* Open for write */
if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened " );
else
printf( "The file 'data2' was opened " );
/* Close stream */
if(fclose( stream2 ))
printf( "The file 'data2' was not closed " );
/* All other files are closed: */
numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u ", numclosed );
}

(5)c语言读取文件内容扩展阅读
使用fgetc函数
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
FILE *stream;
char buffer[81];
int i, ch;
/* Open file to read line from: */
if( (stream = fopen( "fgetc.c", "r" )) == NULL )
exit( 0 );
/* Read in first 80 characters and place them in "buffer": */
ch = fgetc( stream );
for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )
{
buffer[i] = (char)ch;
ch = fgetc( stream );
}
/* Add null to end string */
buffer[i] = '
