这对于发现错误来说是一个很好的问题.没有?至少对初学者好.
#define SIZE 4
int main(void){
int chars_read = 1;
char buffer[SIZE + 1] = {0};
setvbuf(stdin, (char *)NULL, _IOFBF, sizeof(buffer)-1);
while(chars_read){
chars_read = fread(buffer, sizeof('1'), SIZE, stdin);
printf("%d, %s\n", chars_read, buffer);
}
return 0;
}
使用上面的代码,我试图使用重定向从文件中读取./a.out < data
.输入文件的内容:
1line
2line
3line
4line
Run Code Online (Sandbox Code Playgroud)
但是我没有得到预期的输出,而是混合了一些图形字符.有什么问题?
提示:(礼貌Alok)
sizeof('1') == sizeof(int)
所以,改用1 :-)
看一下这篇文章,了解使用fread的缓冲IO示例.
以下程序用%20替换所有空格.编译工作正常,但程序在运行时终止.任何帮助???
#include<iostream>
#include<string>
using namespace std;
void removeSpaces(string url){
int len=url.length();
int i,count=0;
while(i<=len){
if(url[i]==' ')
count++;
i++;
}
int length2=len+(count*2);
string newarr[length2];
for(int j=len-1;j>=0;j--){
if(url[j]==' ')
{
newarr[length2-1]='0';
newarr[length2-2]='2';
newarr[length2-3]='%';
length2=length2-3;
}
else
{
newarr[length2-1]=url[j];
length2=length2-1;
}
}
cout<<"\nThe number of spaces in the url is:"<<count;
cout<<"\nThe replaced url is:"<<newarr;
}
int main(){
string url="http://www.ya h o o.com/";
removeSpaces(url);
}
Run Code Online (Sandbox Code Playgroud) 我像这样安装了boto:python setup.py install; 然后当我在shell上启动我的python脚本(从boto导入模块)时,出现如下错误:ImportError:没有名为boto.s3.connection的模块
如何解决这个问题?
我是道德黑客世界的新手,其中一个最重要的事情是堆栈溢出,无论如何我编写了一个易受攻击的C程序,它有一个char名称[400]语句,当我尝试用401A运行程序时它不会不要溢出,但是我所遵循的这本书说它必须溢出而且逻辑意义如此说,那么什么是错的?
# include <stdio.h>
int x = 5;
int main(void)
{
int x = 7;
printf("output = %d\n", x);
}
Run Code Online (Sandbox Code Playgroud)
上面的程序显示输出为7.如何在c中打印5?
感谢名单...
我正在寻找一种在javascript中搜索生成的网页源(document.innerHTML)以获取字符串的方法.
我不想使用window.find(),因为我可能也要查找id或名称.
任何帮助,将不胜感激.
谢谢
作为更大程序的一部分,我有一种计算多项式导数系数的方法.但那不是问题:).在下面的代码中,如果我跳过realloc()调用,我会得到我期望的结果(*coef).使用realloc将其中一个元素设置为0.这是我的错,还是realloc()行为实际上是意外的?
void derive (double **coef, int size)
{
int i;
if (size<2)
free_vector (*coef);
else
{
for (i=0; i<size-1; ++i)
{
(*coef)[i] = (*coef)[i+1]*(i+1);
}
(*coef) = realloc ((*coef), size-1);
}
}
Run Code Online (Sandbox Code Playgroud)
如果运行它,我还会附上测试程序的完整源代码...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *new_vector (int size)
{
double *vec = calloc (size, sizeof (double));
return vec;
}
void free_vector (double *vec)
{
if (vec)
{
free (vec);
vec = NULL;
}
}
void write_vector (FILE *f, double *vec, int size)
{
int i; …
Run Code Online (Sandbox Code Playgroud)