【发布时间】:2018-04-24 08:53:41
【问题描述】:
我需要获取用户输入的文件并将其乘以另一个文件。我知道该怎么做。
问题是一个文件是数组,另一个是矩阵。
我需要扫描矩阵的第一行以找到矩阵的大小,然后我需要从文件中动态分配矩阵和数组。
这是我目前所拥有的:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int row1, col1;
//These values need to be pulled from the first file//
char filename1[100];
//Setting the file name for entry and setting the limit to 100//
FILE* fp1;
//FILE must be set as a pointer (FILE must also be capitalized)//
printf("Enter file name including file extension: \n");
//This will pull in the name entered by the user//
scanf("%s", filename1);
//Scans in the name of the first file//
fp1 = fopen(filename1, "r");
//This will open the file as entered by the user//
if (fp1 == NULL)
{
printf("\nError, file not found\n");
exit(0);
}
//This is for the first file//
char filename2[100];
//Setting the file name for entry and setting the limit to 100//
FILE* fp2;
//FILE must be set as a pointer (FILE must also be capitalized)//
printf("Enter file name including file extension: \n");
//This will pull in the name entered by the user//
scanf("%s", filename2);
//Scans in the name of the first file//
fp2 = fopen(filename2, "r");
//This will open the file as entered by the user//
if (fp2 == NULL)
{
printf("\nError, file not found\n");
exit(0);
}
//This is for the second file//
//**I need to now dynamically allocate the input files**//
return 0;
}
也很抱歉,我似乎在发布我的问题后才离开,因为一些成员在 cmets 中分享说我在钓鱼。我不是;我只是没有意识到这个社区有多活跃。感谢您到目前为止的输入。
Here is the screenshot of all I have so far including the files that are going to be read in.
感谢您的建议。我能够找出“fgets”函数,并用它从第一个文件中提取矩阵的大小。有了它之后,动态分配就很容易了。
【问题讨论】:
-
显示输入文件的内容
-
如果你能给出一个最小的可重现的例子会更容易提供帮助。
-
到目前为止,您已经打开了文件...了解数组不是矩阵和矩阵不是数组。 C 中没有 matrix 的定义。您可以选择任何方式来管理和索引存储的数字。通常,如果您在每行动态分配未知数量的元素 - 您将使用指向指针类型的指针,分配 X 指针,然后为每行中的任意数量的值分配存储(验证您从文件中的每一行读取相同的数字),根据需要重新分配指针的数量。
-
@DavidC.Rankin.:我不知道为什么,但我多次观察到 1 个代表用户提出问题 - 然后他们不互动。现在,如果您很幸运能够正确了解问题,那么您将给出解决方案。 OP 将在 3-4 小时后出现 - 将检查哪个提供解决方案(烘焙代码)并使用它。这有时真的非常非常烦人。
-
@coderredoc 我认为这可能与钓鱼代码的历史悠久的闪避有关。这个比大多数人更“填补空白......”。我不知道 - 也许我读错了问题......
标签: c arrays matrix memory-management dynamic-memory-allocation