我正在尝试学习C,但我已经遇到了问题.我认为它是微不足道的,但我需要知道它.我已经写了:
#include <stdio.h>
#include <string.h>
int main()
{
char str_a[20];
strcpy(str_a, "Hello, world!\n");
printf(str_a);
}
Run Code Online (Sandbox Code Playgroud)
一旦我尝试使用以下命令编译它:gcc -g -o char_array2 char_array2.c我收到错误说:
char_array2.c: In function ‘main’:
char_array2.c:9:2: warning: format not a string literal and no format arguments [-Wformat-security]
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
我正在阅读Hacking剥削艺术,有一个例子,我似乎无法正确.尝试编译错误的结果:
./addressof.c: In function ‘main’:
./addressof.c:8:4: warning: format ‘%x’ expects argument of type ‘unsigned int’,
but argument 2 has type ‘int *’ [-Wformat]
#include <stdio.h>
int main() {
int int_var = 5;
int *int_ptr;
int_ptr = &int_var; // Put the address of int_var into int_ptr.
printf("int_ptr = 0x%08x\n", int_ptr);
printf("&int_ptr = 0x%08x\n", &int_ptr);
printf("*int_ptr = 0x%08x\n\n", *int_ptr);
printf("int_var is located at 0x%08x and contains %d\n", &int_var, int_var);
printf("int_ptr is located at 0x%08x, contains 0x%08x, and points to %d\n\n",
&int_ptr, int_ptr, …Run Code Online (Sandbox Code Playgroud) 我创建了一个文件,在用户想要输入的情况下多次打印Hello,world.
#include <stdio.h>
#include <string.h>
int main() {
char message[10];
int count, i;
strcpy(message, "Hello, world!");
printf("Repeat how many times? ");
scanf("%d", &count);
for(i=0; i < count; i++)
printf("%3d - %s\n", i, message);
}
Run Code Online (Sandbox Code Playgroud)
无论输入的数字是多少,它总会导致"堆栈粉碎".这是程序,任何人都可以得出结论为什么这样做?这是检测到堆栈粉碎后发生的"追溯":
sean@blue:~/programming$ ./a.out
Repeat how many times? 12
0 - Hello, world!
1 - Hello, world!
2 - Hello, world!
3 - Hello, world!
4 - Hello, world!
5 - Hello, world!
6 - Hello, world!
7 - Hello, world!
8 - Hello, world!
9 - …Run Code Online (Sandbox Code Playgroud) 我正在阅读K&R,问题是:编写一个程序将其输入复制到其输出中,用一个空格替换一个或多个空格的每个字符串.在我看来,我想我知道我需要做什么,设置一个布尔值来知道我什么时候在一个空间.我尝试过但没有成功.我发现这个代码并且它有效,我正在努力弄清楚是什么阻止了空间被写入.我想我可以拥有它,但我需要澄清.
#include <stdio.h>
int main(void)
{
int c;
int inspace;
inspace = 0;
while((c = getchar()) != EOF)
{
if(c == ' ')
{
if(inspace == 0)
{
inspace = 1;
putchar(c);
}
}
/* We haven't met 'else' yet, so we have to be a little clumsy */
if(c != ' ')
{
inspace = 0;
putchar(c);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个文本文件,文本内容如下:
so this is where you have been
Run Code Online (Sandbox Code Playgroud)
在"这个"'s'之后,状态变为1,因为我们在一个空间中.空间被写入并读取下一个空格.所以现在我们输入:
while((c = getchar()) != EOF)
{
if(c == …Run Code Online (Sandbox Code Playgroud) 可能重复:
Python中的反引号等价物
我正在寻找在Python中运行终端命令(ls -l)的最佳方法.我已经阅读了有关子流程的内容,但我完全不了解它,如果有人可以尝试让我了解正在发生的事情,我将不胜感激.我需要使用ls -l命令检索一个硬链接号,即!= 1,然后保存此号码以匹配其他地方的目录号码.现在我想知道如何获取硬链接号并使用子进程将其保存到变量(如果有的话,可以使用更好的方法).
这是我到目前为止使用的代码:#!/ usr/bin/python
#tool that resolves time machine directories
import os
#create output file
os.chdir("/home/sean/Desktop")
hard_link_number = open('hardLinkNumber.log', 'w')
#move into mounted backup (figure out how to remove xe2 etc)
os.chdir("/mnt/Backups.backupdb/stuart dent\xe2\x80\x99s MacBook Pro/2010-08-10-160859/MAc")
#find hard link data
print>>hard_link_number, os.system("ls -la")
hard_link_number.close()
Run Code Online (Sandbox Code Playgroud)
os.system("ls -la")输出我需要的信息,但不会将其保存到我创建的文件中.我在别处读到os.system不会输出数据.