我正在尝试计算char数组中的字符数,包括直到字符串结尾的空格.
以下编译但不返回正确的值,我正在尝试使用指针算法来通过我的数组进行交互.
int numberOfCharsInArray(char* array) {
int numberOfChars = 0;
while (array++ != '\0') {
numberOfChars++;
}
return numberOfChars;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢.
显然我试图从cstring获得相当于length()但使用简单的char数组.
当然,如果我的原始数组不是null终止,这可能会导致一个非常大的值返回(我猜).
我遇到了一些与C/C++相关的问题:假设我有一些课程
class Demo
{
int constant;
public:
void setConstant(int value)
{
constant=value;
}
void submitTask()
{
// need to make a call to C-based runtime system to submit a
// task which will be executed "asynchronously"
submitTask((void *)&constant);
}
};
// runtime system will call this method when task will be executed
void func(void *arg)
{
int constant= *((int *)arg);
// Read this constant value but don't modify here....
}
Run Code Online (Sandbox Code Playgroud)
现在在我的应用程序中,我做了这样的事情:
int main()
{
...
Demo objDemo;
for(...)
{
objDemo.setConstant(<somevalue>); …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc,char *argv[])
{
int fd;
int i=1;
for(i=1;i<argc;++i)
{
char temp;
fd=open(argv[i],"O_RDWR");
if (fd==-1)
perror("file:");
while (read(fd,&temp,1)!=EOF)
{
putchar(temp);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我执行./a.out a b.a并且b是我的目录中的文件.我收到一个错误说File exists.该行open(argv[i],"O_RDWR")未打开该文件.
它返回,-1因为文件存在.那怎么应该使用open系统调用打开文件?
我的程序总是将数据写入文件,但是当我在程序完全停止之前关闭它时,最终结果就是没有写入文件.我真的希望能够在没有完全完成的情况下关闭它,那么如何解决这个问题以使其不断保存文件?
ofstream outfile;
outfile.open("text.txt", std::ios::app);
bool done = false;
int info;
while (done == false){
cin>>info;
outfile<<info;
cout<<info<<"Choose different info";
if(info == 100){
done = true;
}
}
outfile.close();
Run Code Online (Sandbox Code Playgroud)
这显然只是一个例子,但它与我的实际代码非常相似.
编辑:当我说关闭时我的意思是杀死它(在控制台的右上方击中红色X)
我试图将.csv文件下载到一个数组中,然后使用Text :: CSV逐行解析每个列.我有以下内容:
my @file = get("http://www.someCSV.com/file.csv") or warn $!;
my $CSV = Text::CSV->new();
$CSV->sep_char (',');
for ( @file ) {
$CSV->parse($_) or warn $!;
my @columns = $CSV->fields();
print $columns[0] . "\n";
}
Run Code Online (Sandbox Code Playgroud)
我认为将CSV文件放入数组并从那里解析会更有效率,而不是下载文件,保存文件然后将其拖入文件句柄.但是,上面的代码不起作用,我不明白为什么.我得到"警告:test.pl上的错误"; 至少可以说,不是很有帮助.
这更适合学习.我不必这样做,但它只是困扰我为什么我不能使用Text :: CSV与数组.