【问题标题】:I can't token my char how can I get there?我无法标记我的 char 我怎样才能到达那里?
【发布时间】:2014-07-16 12:29:12
【问题描述】:
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 文件并保存 char 指针 我标记这个字符并保存 例如

那里有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};

但我不知道该怎么做:(

你能帮帮我吗?

【问题讨论】:

  • 你的fx[c] 是什么?你是在strtok-ing 之前从你的文件中读取它吗?
  • i = i++; 是未定义的行为
  • 现在我正在编辑我的代码。
  • main 必须返回 int
  • 你从未为fx分配内存

标签: c++ c file io token


【解决方案1】:

使用向量和字符串

#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;
}

【讨论】:

  • 嗯...我认为这与我的想法不同.. 它不区分字符 *a[3] = {123 654 8765 4213, 321 565 4687 8765, 652 126 6874 3215};...
  • @user3844979 我在 C 中添加了一个示例。
  • 它有错误。 fgets 有错误! :(我的编程水平太低了....:(我不知道如何解决它..
  • @user3844979 你有什么错误?如果示例的文件名不同。 (已编辑。"data.txt" --> "fx.txt"
  • 调试断言失败!程序:...cuments\visual studio 2013\Projects\fxToNum\fxToNum.exe 文件:f:\dd\vctools\crt\crtw32\stdio\fgets.c 行:57 表达式:(str!=NULL)
【解决方案2】:

如果没有限制,使用字符串,获取boost然后就可以使用了

vector<string>splitString;
boost::split(splitString,a[i],boost::is_any_of(" "));

这样,字符串向量 (splitString) 中的每个元素都将包含第 i 行的一个“数字”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多