C 我试图读取存储在缓冲区中的值,但它却向我显示了地址?
我想打印位置中的值,而不是地址。。 当我使用断点运行程序时,它会增加值,但不会打印地址中包含的值C 我试图读取存储在缓冲区中的值,但它却向我显示了地址?,c,file-handling,fread,C,File Handling,Fread,我想打印位置中的值,而不是地址。。 当我使用断点运行程序时,它会增加值,但不会打印地址中包含的值 #include <stdio.h> #include <stdlib.h> #include <string.h> #include "conio.h" int main() { char ch; char buffer[100]; char* p; p =
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "conio.h"
int main()
{
char ch;
char buffer[100];
char* p;
p = buffer;
FILE *fp;
fp = fopen("D:\\Telenor_Short_01.vts","rb");// binary mode
fseek(fp,0,SEEK_END); //sets the file position of the stream to the given offset
int size=ftell(fp); //returns the current file position of the given stream.
printf("size of file is :%d\n",size);
if( fp == NULL ) //error checking
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
fread(p,1,100,fp);
for (int i=0;i<100;i++)
{
printf("%04x\n",*p);
p++;
}
fclose(fp);
/*printf("The contents of %s file are :\n", file_name);*/
/*int i;
while( ( ch = fgetc(fp) ) != EOF )
{
printf("%02X ",ch);
if( !(++i % 16) ) putc('\n', stdout);
}
fclose(fp);
putc('\n', stdout);*/
_getch();
return 0;
}
#包括
#包括
#包括
#包括“conio.h”
int main()
{
char ch;
字符缓冲区[100];
char*p;
p=缓冲区;
文件*fp;
fp=fopen(“D:\\Telenor\u Short\u 01.vts”,“rb”);//二进制模式
fseek(fp,0,SEEK_END);//将流的文件位置设置为给定偏移量
int size=ftell(fp);//返回给定流的当前文件位置。
printf(“文件大小为:%d\n”,大小);
if(fp==NULL)//错误检查
{
perror(“打开文件时出错。\n”);
退出(退出失败);
}
fread(p,1100,fp);
对于(int i=0;i首先,在fopen之后立即检查fp是否为NULL。其次,在fread之前查找文件的开头。最后,最重要的是,检查fread的返回值,因为这是读取到缓冲区中的元素数。它可能小于缓冲区
fp = fopen("D:\\Telenor_Short_01.vts","rb");// binary mode
if( fp == NULL ) //error checking
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
fseek(fp,0,SEEK_END); //sets the file position of the stream to the given offset
int size=ftell(fp); //returns the current file position of the given stream.
printf("size of file is :%d\n",size);
fseek(fp, 0, SEEK_SET);
int nread = fread(p, 1, 100, fp);
for (int i=0; i<nread; i++)
{
printf("%04x\n", *p);
p++;
}
fclose(fp);
要按块读取大文件,请执行以下操作:
fseek(fp, 0, SEEK_SET);
char buf[4096];
int nread;
int i;
while (1) {
nread = fread(buf, 1, 4096, fp);
for (i=0; i<nread; i++)
{
printf("%02x\n", buf[i]);
}
if (nread < 4096)
break;
}
fclose(fp);
fseek(fp,0,SEEK\u集);
char-buf[4096];
国际nread;
int i;
而(1){
nread=fread(buf,14096,fp);
对于(i=0;i首先,在fopen之后立即检查fp是否为NULL。其次,在fread之前查找文件的开头。最后,最重要的是,检查fread的返回值,因为这是读取到缓冲区中的元素数。它可能小于缓冲区
fp = fopen("D:\\Telenor_Short_01.vts","rb");// binary mode
if( fp == NULL ) //error checking
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
fseek(fp,0,SEEK_END); //sets the file position of the stream to the given offset
int size=ftell(fp); //returns the current file position of the given stream.
printf("size of file is :%d\n",size);
fseek(fp, 0, SEEK_SET);
int nread = fread(p, 1, 100, fp);
for (int i=0; i<nread; i++)
{
printf("%04x\n", *p);
p++;
}
fclose(fp);
要按块读取大文件,请执行以下操作:
fseek(fp, 0, SEEK_SET);
char buf[4096];
int nread;
int i;
while (1) {
nread = fread(buf, 1, 4096, fp);
for (i=0; i<nread; i++)
{
printf("%02x\n", buf[i]);
}
if (nread < 4096)
break;
}
fclose(fp);
fseek(fp,0,SEEK\u集);
char-buf[4096];
国际nread;
int i;
而(1){
nread=fread(buf,14096,fp);
对于(i=0;iif(fp==NULL)//错误检查
-这应该紧跟在fopen
fseek(fp,0,SEEK_END);
之后的fread()将尝试读取EOF以外的内容,并失败。@EdHeal我这样做了,但仍然遇到问题这是一条注释-您需要这样做。@EdHeal fp=fopen(“D:\\Telenor_Short_01.vts”,“rb”);//二进制模式if(fp==NULL)//错误检查{peror(“打开文件时出错。\n”);退出(退出失败)}if(fp==NULL)//错误检查
-这应该紧跟在fopen
fseek(fp,0,SEEK_END)
fread()s之后,将尝试读取EOF以外的内容,但失败。@EdHeal我这样做了,但仍然遇到问题这是一个注释-您需要这样做。@EdHeal fp=fopen(“D:\\Telenor\u Short\u 01.vts”,“rb”);//二进制模式if(fp==NULL)//错误检查{perror(“打开文件时出错”。\n”);退出(退出失败)}这是可行的,但打印出2个字节,我只需要1个字节,但这不是问题,我在打印过程中添加了02而不是04。。而且&arr[3]与arr+3相同;
当然只适用于char-arr[x],如果它是另一种类型,则只需在arr的地址中添加3个字节。您也可以执行printf(“%04x\n”,*p++);
它将打印您的*p
值,然后再按指针指向的类型的大小递增。@Gatica否,对于整数和结构也是如此,指针算法是根据数组中元素的大小而不是字节来工作的。@fluter现在我想打印大小为185153907的文件的值。。但这次我不在控制台上打印任何内容…我使用以下代码分配大小:char*buffer=(char*)malloc(memory*sizeof(int));int nread=fread(buffer,1185153907,fp);for(int i=0;iSo是否这样做?有什么问题可以解决,但打印2个字节?我只需要1个字节,但这不是问题,我在打印过程中添加了02而不是04...&arr[3]与arr+3相同;
当然只适用于char arr[x],如果它是另一种类型,则只需向arr的地址添加3个字节。您也可以执行printf(“%04x\n”,*p++);
它将打印您的*p
值,然后再按指针指向的类型的大小递增。@Gatica否,对于整数和结构也是如此,指针算法是根据数组中元素的大小而不是字节来工作的。@fluter现在我想打印大小为185153907的文件的值。。但这次我不在控制台上打印任何内容…我使用以下代码分配大小:char*buffer=(char*)malloc(memory*sizeof(int));int nread=fread(buffer,1185153907,fp);for(int i=0;iSo do it?有什么问题吗