小编fcl*_*pez的帖子

将for循环中的值存储到数组中

我想将从for-loop读取的值存储到数组中

char A[];
int x;
int y=5;

for( int i=0; int i =1000; i++) {
   x = x+y;
   // then store/append x as elements of the char array, A.... what is the syntax?
}
Run Code Online (Sandbox Code Playgroud)

c arrays for-loop

1
推荐指数
1
解决办法
8万
查看次数

fprintf,字符串和向量

可能重复:
c ++ - 字符串上的printf打印出乱码

我想写几个字符串来存档.字符串是

37 1 0 0 0 0
15 1 0 0 0 0
33 1 0 0 0 0
29 1 0 0 0 0
18 1 0 0 0 0
25 1 0 0 0 0
Run Code Online (Sandbox Code Playgroud)

我首先想将每一行存储为字符串数组的元素,然后调用相同的字符串数组并将其元素写入文件.

#include <stdio.h>
#include <vector>
#include <string>
using namespace std;

int writeFile() {

  char line[100];
  char* fname_r = "someFile_r.txt"
  char* fname_w = "someFile_w.txt"; 
  vector<string> vec;

  FILE fp_r = fopen(fname_r, "r");
  if(fgets(line, 256,fp_r) != NULL)   {
     vec.push_back(line);
  }

  FILE …
Run Code Online (Sandbox Code Playgroud)

c c++ string printf vector

0
推荐指数
1
解决办法
3894
查看次数

标签 统计

c ×2

arrays ×1

c++ ×1

for-loop ×1

printf ×1

string ×1

vector ×1