- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 C++。
我正在尝试转换这样的文本文件:
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
...
像这样的文件:
int grid[20][30] =
{
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
这两个文本文件只是一个例子,向您展示我正在尝试做的事情。第一个文本文件不会生成第二个文本文件。
我已经记下这段代码:
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[])
{
if ((argc == 1) || (argc != 5)) {
cout << "Usage: format rows columns input_file output_file\n";
return 0;
}
// Number of rows in the image.
int rows = atoi(argv[1]);
// Number of columns in the image.
int columns = atoi(argv[2]);
// Character read from the input file.
char ch;
// Counter to create arrays of "columns" elements.
int colCount = 0;
// Counter for the number of rows added to the grid.
int rowCount = 0;
// Output file.
ofstream fout;
fout.open(argv[4], ios::out);
// Write the header to output file.
fout << "int grid[" << rows << "][" << columns << "] = {";
// Read input file.
fstream fin(argv[3], fstream::in);
while (fin >> noskipws >> ch)
{
if (colCount == 0)
fout << "\n{";
if ((!isspace(ch)) && ((ch == '1') || (ch == '0') || (ch == ','))) {
fout << ch;
colCount++;
}
if (colCount == columns) {
colCount = 0;
rowCount++;
if (rowCount != rows)
fout << "},";
}
}
fout << "}};\n";
fout.close();
return 0;
}
但它似乎从未进入主循环(while (fin >> noskipws >> ch)
)。我在文本文件中得到这个输出:
int grid[365][484] = {}};
我正在使用以下命令行在 Linux (Ubuntu) 上使用 g++ 进行编译:
g++ FormatMatrix.cpp -o format
我做错了什么?
最佳答案
在进入 while 循环之前,检查创建/打开输入流“fin”是否成功。
关于c++ - fin >> noskipws >> ch 始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56289411/
package practicejava; public class Query { public static void main(String[] args) { char
因为我很困惑其中会存储什么。它是 ascii 值还是“z”,因为有些网站说它存储 ascii,有些网站说它存储“z”。链接说答案是“z”:http://cprogrammingworld4u.blog
这个问题在这里已经有了答案: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expec
我无法将字符指针数组 s 的字符存储到字符数组 ch 中: int main() { char *s; s = malloc(1024 * sizeof(char));
我有这个代码: int main() { char ch[15]; cout<
在看《C++ Primer Plus 5th edition》时,看到了这样一段代码: cin.get(ch); ++ch; cout void print_type(cha
这行代码做了什么? char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) } ch:i 是什么意思? 最佳答案 这是@han solo 中提
这行代码做了什么? char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) } ch:i 是什么意思? 最佳答案 这是@han solo 中提
我在关注《C Primer Plus》这本书,遇到这样一段代码: // echo_eof.c -- repeats input to end of file. #include int main(v
这个问题在这里已经有了答案: Difference between int* a and int *a [duplicate] (2 个答案) Why is the asterisk before
我正在运行单个 goroutine 来处理与某些用户相关的消息 channel 。处理消息后,用户状态由 goroutine 更新并存储在数据库中。当对数据库的请求正在进行时,可以将许多消息发送到 c
这是完整的代码: s = 'life is short, stunt it!!?' from string import punctuation tbl = str.maketrans({ord(ch
在 C++ 中,(int) ch 是否等同于 int(ch)。 如果不是,有什么区别? 最佳答案 它们是同一个东西,也和(int)(ch)一样.在 C++ 中,通常首选使用命名转换来阐明您的意图: 使
我在 php 中使用 cURL。注意到每次启动 curl 案例时,人们总是把: $ch=curl_init(); “$ch”是什么意思? curl -F template_invoke_name='s
我正在编写与对齐相关的代码,如果给定的指针正确对齐,却没有标准的功能测试,这让我感到非常惊讶。 似乎互联网上的大多数代码都使用(long)ptr或 reinterpret_cast(ptr)测试对齐,
这个问题已经有答案了: C skipping one command of a function? [duplicate] (2 个回答) 已关闭 5 年前。 scanf("%c", &ch ) sc
然后我遇到了系统调用“write”,我尝试将“putchar”与“write”进行比较。现在,我对上面的代码行感到困惑。 最佳答案 putchar 是一个库函数。它调用 write 系统调用将字符写入
在 codechef 上用于更快输入输出的许多解决方案中,我遇到过这个表达式,但我无法理解它,因为我没有很多经验。 inline int scan( ) { int n=0; int ch=getch
这个问题在这里已经有了答案: What does space in scanf mean? [duplicate] (6 个答案) 关闭 7 年前。 像这样在scanf中加一个空格的目的是什么 sc
我的 spring-boot 应用程序中有以下 logback.xml: ... ...
我是一名优秀的程序员,十分优秀!