为 git 存储库制作 c 文件
Making a c file for a git repository
我对 Ubuntu 和 PuTTY 非常陌生,并在其中放入了一个C++文件,但我的C++文件遇到了问题。我需要程序做的是从 Ubuntu 端输入一个字符串,放入C++程序中,让它计算输入了多少个字符串,然后像这样发回:
./myfile Supplying arguments now
Argument #0: ./myfile
Argument #1: Supplying
Argument #2: arguments
Argument #3: now
Number of arguments printed: 4
所以,当我在下面运行我的程序时,程序会永远持续下去,我无法逐步完成它。是什么原因造成的,为什么和/或我可以做些什么来解决问题?
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int count = 0;
while (*argv[argc] != NULL)
{
count++;
}
cout << count << endl;
system("PAUSE");
return 0;
}
你的代码是一个无限循环,因为你的while
循环总是检查相同的条件。这是因为argc
永远不会更改代码。
你想写的是while (*argv[count] != NULL)
.但是,你的意思也不正确。
- C 不检查数组边界。当您读取超过数组边界时,您不一定会遇到 0 值。您将读取该位置内存中的随机垃圾数据。
- 您无需自己计算参数的数量,因为您已经在变量
argc
中拥有它。
因此,迭代所有命令行参数的更好解决方案是一个for
循环,它将count
从0
递增到argc
。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- 我应该将除 .cpp 以外的其他文件添加到 git 中吗?
- 我被困在尝试克隆需要 cmake 文件和依赖项的 git 存储库中
- git:将同一文件的两个位置保持同步
- git_clone() 克隆 .git 文件夹 (libgit2)
- 如何将 git 版本信息添加到 cython 构建的 .so 文件中?
- 为 git 存储库制作 c 文件
- Qt 在编译时生成包含 Git 哈希的头文件,并使用文件强制重建
- 合并MERGE_HEAD后的Libgit2仍存在于.git文件夹中
- 将git-commit和git-diff作为字符串放入二进制文件中
- Git如何忽略未跟踪的CDT文件
- Visual Studio 在从 GIT 存储库拉取文件时不会通知我文件更改(使用 samba)
- git冲突解决文件并不总是代表源文件中的每一行吗?
- 使用libgit2从git存储库中删除文件
- NetBeans 8.0.2中打开的文件保存在项目文件中,因此在GIT中使其脏
- 如何将git提交编号包含在c++可执行文件中
- 这些辅助文件是否应在 Git 版本控制下
- Git 状态显示奇怪的未跟踪"path_of_filer"文件,如何通过命令行删除