[英]Read each line from a file and split the line into a string and an array in C
Homework - I have an assignment to write a program that will read a file. 作业 -我分配了一个程序来读取文件。 The file looks like this:
该文件如下所示:
B 34 55 66 456 789 78 59 2 220 366 984 132 2000 65 744 566 377 905 5000
I 9000
I 389
Dm
DM
Where B
is build a binary heap from an array of numbers(the numbers following the B. I is insert a number into the array/heap Dm is delete minimum and DM is delete maximum. 其中
B
是从数字数组构建二进制堆(B后面的数字。我是在数组/堆中插入一个数字Dm是删除最小值,而DM是删除最大值。
I have written the code for the heap, and can fill an array with random numbers
. 我已经为堆编写了代码,并且可以用
random numbers
填充数组。 My problem is reading that first line
and parsing it into a string B
and an array
. 我的问题是读取
first line
并将其解析为string B
和array
。
I have tried using the following code but obviously it does not work. 我尝试使用以下代码,但显然不起作用。
char line[8];
char com[1];
int array[MAX] //MAX is pre-defined as 100
FILE* fp = fopen( "input_1.txt", "r" );
if( fp )
{
while( fgets ( line, sizeof(line), fp ) != NULL )
{
sscanf(line, "%s" "%d", &com, &array );
... //Following this, I will have a nested if that will take
each string and run the appropriate function.
if ( strcmp( com, "B" ) == 0 )
{
fill_array( array, MAX );
print_array( array, MAX );
}
I have read for about 6 hours over a total of 3 days and can't find a solution to my problem. 我已经阅读了大约6个小时,总共3天,但无法找到解决我问题的方法。 Any help would be great.
任何帮助都会很棒。
Here's a little program that will open a file, read out 1 line, and split what it finds around spaces: 这是一个小程序,它将打开一个文件,读出1行,并在空格附近分割它找到的内容:
void main()
{
char str[50];
char *ptr;
FILE * fp = fopen("hi.txt", "r");
fgets(str, 49, fp); // read 49 characters
printf("%s", str); // print what we read for fun
ptr = strtok(str, " "); // split our findings around the " "
while(ptr != NULL) // while there's more to the string
{
printf("%s\n", ptr); // print what we got
ptr = strtok(NULL, " "); // and keep splitting
}
fclose(fp);
}
So were I to run this on a file containing: 所以我要在包含以下内容的文件上运行此文件:
B 34 55 66 456 789 78 59 2 220
I could expect to see: 我可以期待看到:
B 34 55 66 456 789 78
B
34
55
66
456
789
78
I think you can see how to modify this to help yourself out. 我认为您可以看到如何修改此设置以帮助自己。
First of all, the line
array should probably have a size greater than 8, probably something like char line[256]
. 首先,
line
数组的大小可能应该大于8,可能类似于char line[256]
。 The same goes for the com
array, which should have at least 3 characters. com
数组也是如此,该数组至少应包含3个字符。
char line[256];
char com[3];
You'll have to read the file line by line using fgets(line, sizeof(line), fp)
and the use strtok()
to separate the command from the command arguments. 您必须使用
fgets(line, sizeof(line), fp)
读取文件fgets(line, sizeof(line), fp)
并使用strtok()
将命令与命令参数分开。
char separators[] = " ";
fgets(line, sizeof(line), fp);
char * p = strtok(line, separators); // p will be a pointer to the command string
strncpy(&com, p, sizeof(com)); // copy the command string in com
// If the command is B, read an array
if (strcmp(com, "B") == 0) {
p = strtok(NULL, separators);
while (p != NULL) {
int value_to_add_to_your_array = atoi(p);
// ... add code to add the value to your array
p = strtok(NULL, separators);
}
// ... call the function that creates your heap
}
// ... add code to handle the other commands
The idea is to read the file line by line, then for each line to first read the command and based on its value determine in which way you should read the rest of the line. 想法是逐行读取文件,然后针对每一行首先读取命令,并根据其值确定应以哪种方式读取其余部分。
In the code above I considered the B
command, for which I added code to read an array. 在上面的代码中,我考虑了
B
命令,为此我添加了代码以读取数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.