gpt4 book ai didi

c++ - 我无法标记我的角色,我该如何到达那里?

转载 作者:行者123 更新时间:2023-11-30 20:26:41 24 4
gpt4 key购买 nike

using namespace std;

char *fx[65537];

void main()
{

FILE *fp;
int i = 0;
int c = 0;
char *token = NULL;
char str1[] = " ";

fp = fopen("fx.txt", "r");
char s[51];
for (c = 0; i <= 65536; c++)
{
token = strtok(fx[c], str1);
while (token != NULL)
{
printf("token : %s", token);
putchar('\n');
token = strtok(NULL, str1);
}
}

while (!feof(fp))
{
i = i++;
fgets(s, 50, fp);
fx[i] = s; //fx 포인터에 s의 임시배열 저장
}

fclose(fp);
}

你好我想制作一个程序,用一行读取 *.txt 文件并保存字符指针我标记这个字符并保存例如

有txt测试.txt

123 654 8765 4213
321 565 4687 8765
652 126 6874 3215

那么,

char *a[3] = {123 654 8765 4213, 321 565 4687 8765, 652 126 6874 3215};
char b[3][4];
char b[0][] = {123, 654, 8765, 4213};
char b[1][] = {321, 565, 4687, 8765};
char b[2][] = {652, 126, 6874, 3215};

但我不知道该怎么做:(

你能帮我吗?

最佳答案

使用 vector 和字符串

#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <string>

int main() {
std::ifstream input("fx.txt");
std::vector<std::string> v((std::istream_iterator<std::string>(input)),std::istream_iterator<std::string>());

for ( std::vector<std::string>::const_iterator iter = v.begin();iter != v.end(); ++iter ) {
std::cout << *iter << std::endl;//print by iterator
}
for(int i=0;i<v.size();++i)
std::cout << v[i] << std::endl;//print by []
}
<小时/>

作者:C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *fx[65537];

int main(){
FILE *fp;
int i = 0, j;
int c = 0;
char *token = NULL;
char str1[] = " \n";
char s[51];

fp = fopen("fx.txt", "r");
while (fgets(s, sizeof(s), fp)){
token = strtok(s, str1);
while (token != NULL) {
fx[i++] = strdup(token);
if(i == 65537)
break;
token = strtok(NULL, str1);
}
}
fclose(fp);

c = i;
for(i=0;i<c;++i)
printf("%s ", fx[i]);
printf("\n\n");

char *(*b)[4] = (char *(*)[4])fx;
for(i=0;i<3;++i){
for(j=0;j<4;++j)
printf("%s ", b[i][j]);
printf("\n");
}
//deallocation
for(i=0;i<c;++i)
free(fx[i]);
return 0;
}

关于c++ - 我无法标记我的角色,我该如何到达那里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24780820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com