#EXTM3U
#EXT-X-DISCONTINUITY-SEQUENCE:0
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:NO
#EXT-X-VERSION:2
#EXT-X-TARGETDURATION:8
#EXTINF:8,
fragment1.ts
#EXTINF:8,
fragment2.ts
#EXTINF:8,
fragment3.ts
#EXTINF:8,
fragment4.ts
#EXTINF:8,
fragment5.ts
#EXTINF:8,
fragment6.ts
#EXTINF:4,
fragment7.ts
#EXT-X-DICONTINUITY
#EXTINF:8,
fragment0.ts
如何将所有写入二维数组的单词传递给结构?
我希望我的结构具有一个#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct words {
char *mas;
}words;
int main(void) {
FILE *fp=fopen("test.txt", "r");
char str[100];
char arr[100][100];
int k=0;
words_in->mas=malloc(sizeof(char)*sizeof(arr));
char *istr;
printf("\nFile text\n\n");
for (int i = 0; i < 3; i++) {
istr = fgets(str, 100, fp);
printf("%s", istr);
for (char* istr = strtok(str, " .\t\n"); istr; istr = strtok(NULL, " .\t\n")) {
strcpy(arr[k++], istr);
}
}
指针数组,而不只是一个指针。或指针的链接列表。或char
个数组。
是否有可能以某种方式为结构和数组动态分配内存?
答案 0 :(得分:1)
如果您想防止两个循环,并且准备牺牲一些内存,则可以采用这种方法
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
typedef struct words {
char **mas;
}words;
int main()
{
FILE *fp=fopen("test.txt", "r");
struct stat statbuf;
fstat(fileno(fp), &statbuf);
long f_size = statbuf.st_size;
words words_in;
words_in.mas = (char**) malloc(sizeof(char*) * f_size); // In worst case each byte is one word.
char fbuf[f_size+1]; // In worst case all bytes form one word ;
long word_count = 0;
while(fscanf(fp,"%s", fbuf) == 1) {
words_in.mas[word_count] = strdup(fbuf);
word_count++;
}
for (long i = 0; i < word_count; i++) {
printf("%s\n",words_in.mas[i]);
}
return 0;
}
输入1
Apple
Bat
Cat
输出1
Apple
Bat
Cat
输入2
AppleBatCat
输出2
AppleBatCat
输入3
Apple Bat Cat
输出3
Apple
Bat
Cat