小编Zak*_*ako的帖子

如何用C(整数)读取文本文件中的特定行

我遇到程序问题(程序的一部分).为了进一步继续,我需要以某种方式读取文件的一行,但必须是一个特定的行.我是C和文件的新手......

我想要做的是要求用户输入他们想要阅读的特定行,然后将其显示给他们.目前,当我尝试从行中打印文本时,它只给出了第1行的文本.请注意,通过文本我的意思是整数,因为该文件由一列中的55个整数组成.所以它看起来像这样:12 18 54 16 21 64 .....

有没有办法达到我的需要?

#include <stdio.h>

FILE *file;

char name[15];
int line;
int text;

file = fopen("veryimportantfile.txt","r");
if(file==NULL)
{
    printf("Failed to open");
    exit(1);
}


printf("Your name: ");
scanf("%s",&name);
printf("\Enter the line number you want to read: ");
scanf("%d",&line);


fscanf(pFile, "%d", &line);
printf("The text from your line is: %d",line);
Run Code Online (Sandbox Code Playgroud)

c file scanf

3
推荐指数
1
解决办法
6079
查看次数

如何将变量传递给另一个函数?

我不知道如何将变量从main()传递给另一个函数.我有这样的事情:

main()
{
  float a, b, c;

  printf("Enter the values of 'a','b' and 'c':");
  scanf("%f %f %f",&a,&b,&c);
}

double my_function(float a,float b,float c)
{
  double d;

      d=a+b+c
      bla bla bla bla
Run Code Online (Sandbox Code Playgroud)

如何将a,b和c从main传递给my_function?现在,程序在scanf()上停止,并在我输入值后直接完成.

我在这里看到了不同的例子,但他们对我帮助不大.

c variables function

-7
推荐指数
1
解决办法
184
查看次数

标签 统计

c ×2

file ×1

function ×1

scanf ×1

variables ×1