C:文件 I/O 循环遍历包含一组数字的文件以找到它们的平方、立方和平方根

标签 c file-io

该程序的目标是从文本文件中读取一组数字,循环遍历文件中的每个数字,找到每个数字的平方、立方和平方根,并将新数字写入输出文本文件。

我已经想出了如何正确地读取和写入文件,但我无法弄清楚如何正确地格式化循环以从输入文件中读取每个数字。

我还尝试在 math.h 中使用“sqrt()”函数,但它没有正常工作我更关心的是首先让循环正常工作。

   #include <stdio.h>
   #include <ctype.h>
   #include <math.h>
   #define SIZE 40

  int main(void)
{
char ch, filename[SIZE]; //variables


int i, n, square, cube , sroot;

FILE *fp;
printf("Please enter the filename to read: ");
gets(filename);


if ((fp = fopen(filename, "r")) == NULL)
{
    printf("Cannot open the file, %s\n", filename);
}
else
{
    while (fscanf(fp,"%d",&n) == 1)
    {
      for(i=0; i<=n; i++)
      {

      square=n*n;
      cube=square*n;
      sroot= sqrt(n);
      }
}
}


fclose(fp);

char filename2 [SIZE];
FILE *fp2;

fprintf(stdout, "Please enter the file name to write in: "); //asks for file that you want to write to
gets(filename2);

if ((fp2 = fopen(filename2, "w")) == NULL) //"w" for writing
{
    printf("Cannot create the file, %s\n", filename2);
}
else
{
    for(i=0; i<=n; i++)
    {
        fprintf(fp2, "%d, %d, %d \n",square, cube, sroot);

    }

}

fclose(fp2); // closing the file
fprintf(stdout, "You are writing to the file, %s is done.\n", filename2);

return 0;

最佳答案

打开这两个文件并检查是否成功。然后,当从输入文件中读取值时,将结果打印到输出文件。关闭这两个文件。
使用 fgets 而不是 gets

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define SIZE 40

int main(void)
{
    char filename[SIZE] = {'\0'};
    char filename2 [SIZE] = {'\0'};
    int n = 0, square = 0, cube = 0 , sroot = 0;
    FILE *fp2;
    FILE *fp;

    printf("Please enter the filename to read: ");
    fgets ( filename, SIZE, stdin);
    filename[strcspn ( filename, "\n")] = '\0';//remove newline

    if ((fp = fopen(filename, "r")) == NULL)
    {
        printf("Cannot open the file, %s\n", filename);
        return 1;
    }

    printf( "Please enter the file name to write in: "); //asks for file that you want to write to
    fgets ( filename2, SIZE, stdin);
    filename2[strcspn ( filename2, "\n")] = '\0';//remove newline

    if ((fp2 = fopen(filename2, "w")) == NULL)
    {
        printf("Cannot open the file, %s\n", filename2);
        fclose ( fp);
        return 2;
    }

    while (fscanf(fp,"%d",&n) == 1)//read a number from input
    {
        square=n*n;
        cube=square*n;
        sroot= (int)sqrt((double)n);
        //print results to output
        fprintf(fp2, "%d, %d, %d \n",square, cube, sroot);
    }
    fclose(fp);
    fclose(fp2);
    printf( "You are writing to the file, %s is done.\n", filename2);
    return 0;
}

关于C:文件 I/O 循环遍历包含一组数字的文件以找到它们的平方、立方和平方根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41003257/

相关文章:

python - alsamixer amixer 转 python 字典格式

c - 读取和写入文件时断言兼容架构的最佳方法?

c - at91sam7 :interrupt for receiving a new a character uart

c - C 数组中偶数索引处的元素之和

Java 保存/打开文件对象

c++ - 如何加速我的 .bmp 类?

php - AWS EC2 vsftpd 将文件/目录所有者显示为数字 (48)

c++ - CreateFile() ERROR_SHARING_VIOLATION 单线程

c - 将 User32 与 gcc 链接

c++ - 带有作用域链的 JS_EvaluateScript