我正在为C/C++库编写.NET适配器,其中方法"bar"采用常规stdio FILE*.是否可以构建一个接口,以便托管代码用户可以传递托管(文件)流?那就是没有创建中间缓冲区和代码来管理数据.还假设bar()读取只会让事情变得更好吗?
// native code
void bar(FILE*);
// interface for managed code
void foo(System::IO::FileStream^ file)
{
FILE* stdio_handle = ???;
bar(stdio_handle);
}
Run Code Online (Sandbox Code Playgroud) 我目前正在忙于一个项目,我必须将一个部分从一个文件复制到另一个文件,所以我使用fread和fwrite为它创建了一个代码.但我遇到了一个问题:出于测试目的,我制作了一个应该复制整个文件的代码,但不知何故,代码创建的副本比原始文件大.请参阅下面的代码
FILE *base_file;
FILE *new_file;
fpos_t curpos;
int tmp;
// Open the base file
fopen_s(&base_file, "C:/base.dat", "rb");
// Open the file which should contain the copy
fopen_s(&new_file, "C:/new.dat", "w");
// Get the filesize
fseek(base_file, 0, SEEK_END);
fgetpos(base_file, &curpos);
fseek(base_file, 0, SEEK_SET);
//Read and copy (it seems to go wrong here)
for(int i = 0; i < curpos; i++){
fread (&tmp, 1, 1, base_file);
fwrite(&tmp, 1, 1, new_file);
}
fclose(base_file);
fclose(new_file);
Run Code Online (Sandbox Code Playgroud)
基本文件为525 kb,新文件为527kb.据我所知,出现此问题的部分是在有7个nullbytes的部分之后,并且副本在某些部分之后添加了一个'0D'(十六进制).在ascii中,'0D'字符是'回车'.我想知道我的复制代码将回车符添加到文件中的原因是什么?据我所知,这个脚本应该只是工作,因为我只是读取基本文件,并直接将其复制到新文件,并且基本文件不包含这些回车.
我使用了system("pause")它stdio.h,它没有错误地工作.当我查看stdio函数时,system()是在stdlib中.它是如何工作的,这是代码?
#include <stdio.h>
int main() {
printf("Hello World\n" );
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我只用#include <stdio.h>在
#include <stdio.h>
void strcat(char* s, char* t);
int main()
{
char str1[12] = "hello";
char str2[] = ",world";
strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
void strcat(char* s, char* t)
{
int i = 0, j = 0;
while(*s != '\0')
i++;
while((*(s + i++) = *(t + j++)) != '\0');
}
Run Code Online (Sandbox Code Playgroud)
但是当我构建它时,控制台输出:
--------------------Configuration: test16 - Win32 Debug--------------------
Linking...
LIBCD.lib(strcat.obj) : error LNK2005: _strcat already defined in test16src.obj
Run Code Online (Sandbox Code Playgroud)
为什么?不strcat中不存在stdio.h?
PS:您能否通过描述与其他文件链接的进度来解释链接错误?
我有以下代码:
FILE *f = fopen('/path/to/some/file', 'rb');
char c;
while((c = fgetc(f)) != EOF)
{
printf("next char: '%c', '%d'", c, c);
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当打印出字符时,在文件的末尾,打印出一个不可渲染的字符,以及ASCII序号-1.
next char: '?', '-1'
Run Code Online (Sandbox Code Playgroud)
应该是什么角色?我知道这不是EOF,因为有一个检查,并且在打印字符后很快,程序SEGFAULT.
lc (C) 和 c / ls (S) 和 s 在printf()功能上有什么区别?为什么ls(S)转换返回-1?
例子:
printf("%C", '??); // -1
printf("%c", '??); // PRINT
printf("%S", "? ans T"); // -1
printf("%s", "? and T"); // PRINT
Run Code Online (Sandbox Code Playgroud)
在 Mac..
我找不到该stdio.h文件。我正在使用 Linux Mint 18.2 XFCE 和 gcc-7.2 编译器。
这是输出find . -type f -name stdio.h
smit@smit-Aspire-5742:/usr/lib/gcc$ find . -type f -name stdio.h
./i686-w64-mingw32/5.3-win32/include/ssp/stdio.h
./i686-w64-mingw32/5.3-win32/include/c++/tr1/stdio.h
./i686-w64-mingw32/5.3-posix/include/ssp/stdio.h
./i686-w64-mingw32/5.3-posix/include/c++/tr1/stdio.h
./x86_64-w64-mingw32/5.3-win32/include/ssp/stdio.h
./x86_64-w64-mingw32/5.3-win32/include/c++/tr1/stdio.h
./x86_64-w64-mingw32/5.3-posix/include/ssp/stdio.h
./x86_64-w64-mingw32/5.3-posix/include/c++/tr1/stdio.h
Run Code Online (Sandbox Code Playgroud)
我不需要 mingw 文件。这是一个我很少使用的交叉编译器。我找不到 gcc-7.2 的stdio.h文件。我是否在错误的目录中查找?
有什么区别:
fread( buf, sizeof buf, 1, file ); // 'file' is valid open 'FILE *'
Run Code Online (Sandbox Code Playgroud)
和
fread( buf, 1, sizeof buf, file );
Run Code Online (Sandbox Code Playgroud)
参数2和3被size_t size, size_t nmemb描述为"fread()读取nmemb数据项,每个字节长度为".我认为读取的最终字节数必须是size * nmemb但由于某种原因,只有第二种语法(大小= 1)对我有效.
我想用两个字符串读取scanf并将它们存储在两个变量中.然而,只有第一个似乎正确阅读,第二个只是返回(null),但我不知道为什么.
int main(int argc, char **argv) {
char *c, *p;
printf("Enter a command with a parameter: ");
scanf("%s%s", c, p);
...
}
Run Code Online (Sandbox Code Playgroud)
有人知道了,出了什么问题?我没有发现任何错误,而且根据我的理解它应该有效.
我一直在玩,并试图为我的大学课程试验C,我找到了我的程序所做的事情,即使我没有告诉它!
我的完整代码:
#include <stdio.h>
int main(void) {
int c;
while ((c = getchar()) != EOF) {
printf("%d\n", (c - '1'));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它的输出看起来像这样:
7
6
-39
Run Code Online (Sandbox Code Playgroud)
现在,谁能告诉我为什么-39正在打印?