我有两个功能。一种复制文本文件,使用"r"和"w"模式进行读写,另一种复制二进制文件,使用"rb"和"wb" 模式。
但如果我要使用这些函数,我必须首先确定我要复制的文件是文本文件还是二进制文件。我想要一个解决方案,使我能够在事先不知道文件性质的情况下成功复制文件。处理这个问题的单个函数。有任何想法吗?
PS:我注意到ctrl + c复制比我的功能更快,因此也欢迎提高功能速度的建议。
#include <stdio.h>
void copyfile_txt(const char *inputfile, const char *outputfile);
void copyfile_bin(const char *inputfile, const char *outputfile);
int main(void)
{
copyfile_txt("file_input.txt", "file_output.txt");
copyfile_bin("ubuntu-14.04.3-desktop-amd64.iso", "Ubuntu.iso");
return 0;
}
long GetFileSize(FILE *fp)
{
fseek(fp, 0L, SEEK_END);
long sz = ftell(fp);
fseek(fp, 0L, SEEK_SET);
return sz;
}
void copyfile_txt(const char *inputfile, const char *outputfile)
{
static FILE *fp_I;
static FILE *fp_O;
fp_I = fopen(inputfile, "r");
if (!fp_I) { …Run Code Online (Sandbox Code Playgroud) 我有64位的Windows 10,我花了很多时间在屏幕后面编程.我需要不时休息一下,并通过使屏幕变黑来限制屏幕光线/辐射与我的头部碰撞,就好像关掉.
我能做的就是掉到登录界面,但我需要看到黑色才能放心!我真正希望实现的是你在不活动时获得的黑屏.可以通过编程方式进行吗?
这是我到目前为止的代码:
#include <Windows.h>
#define KEY_DOWN(key) ((::GetAsyncKeyState(key) & 0x80000) ? 1 : 0)
#define KEY_UP(key) ((::GetAsyncKeyState(key) & 0x80000) ? 0 : 1)
int main(void)
{
// Hide the console window
HWND hWnd;
AllocConsole();
hWnd = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(hWnd, 0);
//Press ctrl + alt + 'L' to lock / Press ctrl + 'E' to terminate the program
while (1)
{
if (::GetAsyncKeyState('L') == -32767)
{
if (KEY_DOWN(VK_CONTROL) && KEY_DOWN(VK_MENU))
LockWorkStation();
}
if (::GetAsyncKeyState('E') == -32767)
{
if (KEY_DOWN(VK_CONTROL))
return …Run Code Online (Sandbox Code Playgroud) 可能是一个愚蠢的问题.我注意到在Linux机器上运行一个简单Hello World程序时执行时间的差异C(虽然它不是语言特定的).
程序:
#include<stdio.h>
#include<time.h>
int main()
{
clock_t begin, end;
double time_spent;
begin = clock();
printf("%s", "Hello World\n");
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("%f\n", time_spent);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
O/P:
$ ./hello
Hello World
0.000061
$ ./hello
Hello World
0.000057
$ ./hello
Hello World
0.000099
Run Code Online (Sandbox Code Playgroud)
这是在四核机器上测试的,平均负载为0.4,并且有足够的可用内存.虽然差别很小,但背后的原因可能是什么?
#include<stdio.h>
void main()
{
float a = 2.3;
if(a == 2.3) {
pritnf("hello");
}
else {
printf("hi");
}
}
Run Code Online (Sandbox Code Playgroud)
它在输出中打印"hi",或者我们可以说如果条件得到假值.
#include<stdio.h>
void main()
{
float a = 2.5;
if(a == 2.5)
printf("Hello");
else
printf("Hi");
}
Run Code Online (Sandbox Code Playgroud)
打印你好.
该printf()函数使用格式说明符%s进行打印char *.该标准未指定如何char实现为有符号或无符号.
因此,当char实施为已签名char,我们用于%s打印时unsigned char *,这样做是否安全?
在这种情况下我们应该使用哪种格式说明符?
我的目标不是编写C编译器,但我确实需要C编程语言的完整语法.这将允许我编写程序来更轻松地格式化,管理和分析C程序和库.为了达到这个目的,我除了能够掌握语言的整个语法之外别无选择.
语法应清楚地说明什么是有效的和什么是无效的.考虑以下代码行:
int (x) = 0;
Run Code Online (Sandbox Code Playgroud)
看到这个声明的AC程序员可能对它的有效性犹豫不决,直到他试图编译它,他可能不知道它实际上是有效的C.当然,很容易说它等同于int x = 0;并且周围的括号括起来.x是多余的,但对于第一次看到它是否被允许的程序员来说,这是不明确的.
这是关于语言的完整语法我需要的详细程度.实现者必须足以使用它来编写可编译任何C代码的编译器,即使我的目的不是编写编译器,但我的项目需要完整的语法细节.
为了获得空终止字符串的长度,我们只是简单地写len = strlen(str),我经常在SO帖子上看到int例如,要获得数组的大小,你需要自己跟踪它,这就是我正常做的事情但是,我有一个问题,我们是否可以通过使用某种方式来获取大小write permission check,检查我们是否具有对内存块的写入权限?例如 :
#include <stdio.h>
int getSize(int *arr);
bool permissionTo(int *ptr);
int main(void)
{
int arr[3] = {1,2,3};
int size = getSize(arr) * sizeof(int);
}
int getSize(int *arr)
{
int *ptr = arr;
int size = 0;
while( permissionTo(ptr) )
{
size++;
ptr++;
}
return size;
}
bool permissionTo(int *ptr)
{
/*............*/
}
Run Code Online (Sandbox Code Playgroud) 我有点像C的初学者,所以请原谅任何粗鲁的错误!
我试图用printf()打印一些格式化的值,但它在输出的中间开始换行,即使没有'\n'!问题出在442行的list_topic函数中.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
typedef struct
{
int t_id;
char t_name[35];
char date_creation[30];
char last_post_date[30];
int sub_users;
int num_posts;
char t_desc[500];
} topic;
typedef struct
{
int u_type;
int t_maxreach;
int u_sub_topic[15];
char u_name[16];
char u_pass[16];
} utilizador;
void instalar();
void menu_inicial();
void menu_admin();
void menu_topic();
void menu_user();
void login();
void options();
void topic_gest();
void user_gest();
void stats();
void new_topic();
void alt_topic();
void rmv_topic();
void list_topic();
void new_user(); …Run Code Online (Sandbox Code Playgroud) 我不知道之间的区别vector<int> v[]和vector<vector<int>> v
vector<int>v[] = {{ 0, 1 }, { 2, 3 }};
v[0][0] = 4;
v[0][1] = 2;
v[0].push_back(4);
for (int i = 0; i < v[0].size(); i++)
cout << v[0][i] << endl;
cout << v[1][0] << endl;
Run Code Online (Sandbox Code Playgroud)
输出:4 2 4 2
vector<vector<int>> v = { {0, 1}, {2, 3} };
v[0][0] = 4;
v[0][1] = 2;
v[0].push_back(4);
for (int i = 0; i < v[0].size(); i++)
cout << v[0][i] << endl;
cout << v[1][0] << …Run Code Online (Sandbox Code Playgroud) 我有一个由安全编码激发的简单问题。
我遇到的情况下我必须分配除法的余数或的负值0到一个int变量,所以就用了以下符号:
int a = -(b % c);
Run Code Online (Sandbox Code Playgroud)
其中b和c都是正整数。表达(b % c)将产生一个正数或0与a将被分配的结果的负值。
如果(b % c)yield 0,-0评估结果是什么?0还是负面的0?如果后者发生,会有什么影响?
PS我不知道负面0是什么。