我正在尝试将一些字符串写入文件.编译时没有任何警告,但是当我运行a.out时会出现段错误.但它会创建目标文件.我是C的新手,所以我为我的格式和其他缺点道歉.这是我的代码:
#include<stdio.h>
int main (void)
{
FILE *fp_out;
char str1[]="four score and seven years ago our";
char str2[]="fathers broughy forth on this continent,";
char str3[]="a new nation, concieved in Liberty and dedicated";
char str4[]="to the proposition that all men are created equal.";
fp_out=fopen("my_file", "w");
if(fp_out!=NULL)
{
fprintf(fp_out,"%s\n", str1[100]);
fprintf(fp_out,"%s\n", str2[100]);
fprintf(fp_out,"%s\n", str3[100]);
fprintf(fp_out,"%s\n", str4[100]);
fclose(fp_out);
}
else
printf("The file \"my_file\" couldn't be opened\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我刚开始学习C,在一本书的项目中它给了我这段代码:
float fahrenheitFromcelcius (float celcius){
float fahrenheit = celcius *1.8 +32.0;
printf("%f celcius is %f fahrenheit\n", celcius , fahrenheit);
return fahrenheit;
}
Run Code Online (Sandbox Code Playgroud)
为什么我需要返回华氏度.我以为你回来检查一切是否正常或停止功能
到目前为止,我在堆栈溢出时发现了这个,但这只打印出当前目录而不是子目录.提前致谢!
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
Run Code Online (Sandbox Code Playgroud) 我这里有一个程序,它总是将数字的数字相加,直到我得到一个数字.例如,给定num = 38,过程如下:3 + 8 = 11,1 + 1 = 2.由于2只有一位数,所以返回它.
问题是,返回main的值是垃圾,但是在从函数addDigits()返回值之前,它是正确的值.谁能在这看到我的错误?这让我疯狂.提前谢谢了.
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int addDigits(int num)
{
vector <int> nums;
if (num <10)
{
return num;
}
int rem, temp = 0;
while (num > 9)
{
temp = num /10;
rem = num %10;
nums.push_back(rem);
num = temp;
}
nums.push_back(num);
int total = nums[0];
for (int i = 1; i < nums.size(); i++)
{
total += nums[i];
}
cout<<"total :"<<total<<endl;
if …Run Code Online (Sandbox Code Playgroud) 我必须为这段代码绘制进程树:
int main ()
{
int i;
for(i = 0; i < 3; i++)
if(fork() > 0)
fork();
return 0;
}
Run Code Online (Sandbox Code Playgroud)