- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究 C 字符串和流以更好地理解它们。我有这个测试程序来从输入文件读取固定大小的数据 block 到缓冲区,将缓冲区内容存储在中间存储中(在这种情况下,我希望存储能够存储三个不同的“读取”)和然后将读取的字符串和中间存储中的字符串之一写入输出文件。
关于此的注意事项:在每次迭代中,我只使用中间存储的两个第一个位置,并将第二个“存储的字符串”写入文件。
代码:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 3
#define BUFFER_SIZE 5
int main(int argc, char** argv) {
FILE* local_stream_test = fopen("LOCAL_INPUT_FILE","r");
FILE* local_output_test = fopen("LOCAL_OUTPUT_TEST","w");
if(!local_stream_test) {
puts("!INPUT FILE");
return EXIT_FAILURE;
}
if(!local_output_test) {
puts("!OUTPUT FILE");
return EXIT_FAILURE;
}
char my_buffer[BUFFER_SIZE];
char test[SIZE];
char* test2[SIZE];
memset(my_buffer,0,sizeof(my_buffer));
memset(test,0,sizeof(test));
memset(test2,0,sizeof(test2));
int read = fread( my_buffer, sizeof(my_buffer[0]), sizeof(my_buffer)/sizeof(my_buffer[0]), local_stream_test );
printf("FIRST READ TEST: %d\n",read);
printf("\tMY_BUFFER, SIZEOF: %lu, STRLEN: %lu\n",sizeof(my_buffer),strlen(my_buffer));
fwrite(my_buffer,sizeof(my_buffer[0]),/*strlen(aux)*/ read,local_output_test);
char* aux_test = strdup(my_buffer);
printf("\tAUX_TEST STRLEN: %lu, ## %s\n",strlen(aux_test), aux_test);
free(aux_test);
aux_test = NULL;
while(read > 0) {
if(feof(local_stream)) {
puts("BYE");
break;
}
read = fread( my_buffer, sizeof(my_buffer[0]), sizeof(my_buffer)/sizeof(my_buffer[0]), local_stream_test );
aux_test = strdup(my_buffer);
if(!aux_test) {
puts("!AUX_TEST");
break;
}
printf("READ TEST: %d\n",read);
printf("\tMY_BUFFER, SIZEOF: %lu, STRLEN: %lu\n",sizeof(my_buffer),strlen(my_buffer));
printf("\tAUX_TEST, SIZEOF: %lu, STRLEN: %lu ** SIZEOF *AUX_TEST: %lu, SIZEOF AUX_TEST[0]: %lu\n",sizeof(aux_test),strlen(aux_test),sizeof(*aux_test),sizeof(aux_test[0]));
fwrite(aux_test,sizeof(aux[0]),/*strlen(aux)*/ read,local_output_test);
printf("** AUX_TEST: %s\n",aux_test);
test2[0] = aux_test;
test2[1] = aux_test;
test2[1][3] = toupper(test2[1][3]);
fwrite(test2[1],sizeof(test2[1][0]),read,local_output_test);
printf("\n** TEST2[0] SIZEOF: %lu, STRLEN: %lu, TEST2[0]: %s\n",sizeof(test2[0]),strlen(test2[0]),test2[0]);
printf("\n** TEST2[1] SIZEOF: %lu, STRLEN: %lu, TEST2[1]: %s\n",sizeof(test2[1]),strlen(test2[1]),test2[1]);
strcpy(test2[1],aux_test);
printf("** COPIED TEST2[1]: %s\n",test2[1]);
free(aux_test);
aux_test = NULL;
puts("*******************************************");
}
return EXIT_SUCCESS;
}
输入文件:
converts a byte string to a floating point value
converts a byte string to an integer value
converts a byte string to an integer value
当打印字符串时,我在第二次读取后在它的末尾得到了额外的垃圾值。这是第一次、第二次和第三次从文件中读取的 stdout
中的输出:
FIRST READ TEST: 5
MY_BUFFER, SIZEOF: 5, STRLEN: 5
AUX_TEST STRLEN: 5, ## conve
READ TEST: 5
MY_BUFFER, SIZEOF: 5, STRLEN: 5
AUX_TEST, SIZEOF: 4, STRLEN: 5 ** SIZEOF *AUX_TEST: 1, SIZEOF AUX_TEST[0]: 1
** AUX_TEST: rts a
** TEST2[0] SIZEOF: 4, STRLEN: 5, TEST2[0]: rts a
** TEST2[1] SIZEOF: 4, STRLEN: 5, TEST2[1]: rts a
** COPIED TEST2[1]: rts a
*******************************************
READ TEST: 5
MY_BUFFER, SIZEOF: 5, STRLEN: 13
AUX_TEST, SIZEOF: 4, STRLEN: 13 ** SIZEOF *AUX_TEST: 1, SIZEOF AUX_TEST[0]: 1
** AUX_TEST: byte▒▒▒▒
** TEST2[0] SIZEOF: 4, STRLEN: 13, TEST2[0]: byTe▒▒▒▒
** TEST2[1] SIZEOF: 4, STRLEN: 13, TEST2[1]: byTe▒▒▒▒
** COPIED TEST2[1]: byTe▒▒▒▒
让我烦恼的是,当垃圾值开始出现时,字符串的长度大于从文件中读取的字节:13
与 5
.我玩过 BUFFER_SIZE
但在打印到 stdout
时我总是得到垃圾值,除非大小足以一次性读取文件。
例如,BUFFER_SIZE
等于 500
,这是 stdout
中的输出:
FIRST READ TEST: 135
MY_BUFFER, SIZEOF: 300, STRLEN: 135
AUX_TEST STRLEN: 135, ## converts a byte string to a floating point value
converts a byte string to an integer value
converts a byte string to an integer value
BYE
生成的输出文件:
BUFFER_SIZE = 5
converts arts a byte byTe stri stRing tong To a fl a FloatinoatIng poig pOint vant Value
clue
converonvErts a ts A byte bytE strinstrIng to g tO an inan IntegertegEr valu vaLue
cone
cOnvertsverTs a by a Byte stte String rinG to anto An inte inTeger vger value
aluE
BUFFER_SIZE = 500:与输入文件相同。
所以,我正在访问越界内存,对吗?但是哪里?我找不到这个问题的根源(很可能我对如何使用 C 字符串有误解)。
附言:
我读了here也许我的问题是我忘记在字符串末尾添加 NULL 标记。正在做:
test2[0] = aux_test;
test2[0][ strlen(aux_test)+1 ] = '\0';
/* OR THIS */
test2[0][read+1] = '\0';
产生相同的结果。
最佳答案
您的部分问题是您正在读取数组范围之外的内容,并且 fread()
当然不会 null 终止任何东西。
例如:
printf("\tMY_BUFFER, SIZEOF: %lu, STRLEN: %lu\n",sizeof(my_buffer),strlen(my_buffer));
您将 5 个字节的数据读入一个 5 字节大小的数组中。 strlen()
报告 5;幸运的是,超出数组末尾的第一个字节恰好是零字节,但由于它在数组之外,因此您在该点调用了未定义的行为(即使您得到了预期的答案)。
在循环中,在第一次迭代中,toupper()
大小写转换为空白,但不会改变它。 test2[0]
和 test2[1]
两者都指向同一个字符串,所以如果 toupper()
做了任何事情,都会影响这两个指针指向的值。
当垃圾值“出现”时,您已将非零字节放入 my_buffer
结束后的数据中, 和 strlen()
读取那些非零字节,直到到达零字节。因此,问题都是由于无法确保您的字符缓冲区在分配的长度内以空终止。当您调用未定义的行为时,可能会发生奇怪的事情。
请注意,如果您使用 printf("<<%.*s>>\n", read, my_buffer);
您只会打印已读取的数据字节。
你问的是:
test2[0] = aux_test;
test2[0][ strlen(aux_test)+1 ] = '\0';
/* OR THIS */
test2[0][read+1] = '\0';
您正在访问超出所提供内容末尾的一个字节。根据定义,strlen(str)
返回第一个数字 len
这样 str[len] == '\0'
.当你写 test2[0][[strlen(aux_test)+1] = '\0';
因此,您要在字符串中第一个 null 的末尾之后写入一个字节。 test2[0][read+1] = '\0';
赋值,假设你刚刚读取了 5 个字节,覆盖了 test2[0][6]
, 但读取的数据的最后一个字节在 test2[0][4]
中, 所以你没有改变 test2[0][5]
(目前尚不清楚您是否被允许这样做)。
test2[0][strlen(aux_test)] = '\0'; // No-op, but safe
test2[0][read] = '\0'; // If you left enough space, null terminates the input
关于c - 打印到标准输出和写入文件时 C 字符串中的垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19770920/
有没有更好的方法用 PHP 将数据输出到 html 页面? 如果我想在 php 中用一些 var 制作一个 div,我会写类似的东西 print (''.$var.''); 或 echo "''.$v
我可以使用 java awt print 来打印文档/文件而不是使用 javax print 吗?我发现在 java awt print 中有一个选项可以使用 AttributedString 将内容
目前我通过以下方式运行 R 脚本: R --slave argument1 argument2 ... 我想知道 R 中关于如何退出脚本并发出警告的最佳实践,q() 会这样做吗? if(!file.
谁能告诉我如何编写一个程序,用 gcc 编译时打印 c ,用 g++ 编译时打印 c++? 最佳答案 #ifdef __cplusplus printf("c++\n"); #else
我需要支持在 KitKat 设备上打印,但我的目标 SDK 是 13(无法更改)。 特别是我需要打印一个 webview。 这是用于打印 webview 的 API: http://developer
我正在尝试创建一个简单的函数,其中 python 将根据您的年份输入计算年龄。我已经尝试了几种方法,但我没有运气 atm。 附:对不起,我是新手。 ame = input(" Enter your n
JavaFX 2.0 是否支持打印?我有一个文本区域,我从中获取文本然后我想打印它,但似乎没有这个功能。 当然,这里我说的是打印到打印机。 :) 最佳答案 尚不支持。作为一种解决方法,您可以使用 Ja
我试图找出printOn的重点。我查看了一些实现它的类,看起来它只是帮助打印不同数据类型的单位。这是准确的吗? 如果是这样,有人能指出我如何为我自己的类(class)实现这一点的正确方向吗?我将在可能
我无法让 IE 打印我的 Canvas (使用 excanvas 生成)...我使用的是最新版本的 excanvas。 http://dl.dropbox.com/u/997831/canvas.ht
我搜索了很多但没有人回答我的问题,我读到在这样的信号处理程序中使用 cout 是不安全的: void ctrlZHandler(int sig_num) { //SIGTSTP-18
我有兴趣打印一系列查询。我有以下代码。 start = datetime.datetime(2012, 2, 2, 6, 35, 6, 764) end = datetime.datetime(201
public class javaClass { public static void main(String [] arg) { String row1 = "A____A"
我需要写入前一行的命令,例如不带\n 的 print()。 下面是一些示例代码: a=0 print("Random string value") if a==0: print_to_prev
我有一个使用 UIKit 和 Objective C 的旧 iOS 应用程序,我目前正在将其移植到 SwiftUI 和 Swift。一切都很顺利,我喜欢 Swift 和 SwiftUI。该应用程序已经
我创建了一个求和函数,它接受一个开始编号和一个结束编号,并返回这两点之间的总和答案 def print_sum_equations(start_number,end_number):
在 Perl 6 中,print 和有什么区别? , put和 say ? 我怎么看 print 5不同,但 put 5和 say 5看起来一样。 最佳答案 put $a就像 print $a.Str
我正在使用 here 中的 getOrgChart 库,我正在尝试打印整个图表,而不仅仅是可见部分。不幸的是,当使用标准库打印功能时,它只会打印出第一部分,而我不知道如何打印整个图表(该图表相当宽,大
我制作了一个非常适合 A4 页面的 View 。现在我想打印它。请注意,我没有使用drawRect或类似的东西,只是一个带有 subview 和文本标签的普通 View 。我的问题是,我对该 View
由于 Cocoa-Java 已弃用,我正在将 Cocoa-Java 代码迁移到 Cocoa + JNI。该代码打印存储在文件中的图像。新的 Cocoa 代码基本上是: NSImage *image =
这个问题已经有答案了: Printing a TDBGrid (4 个回答) 已关闭 6 年前。 如何在不安装或下载组件的情况下打印 DBGrid? 或者 如何将 DBGrid 的数据放入 RichE
我是一名优秀的程序员,十分优秀!