我有一个包含字符串的结构.像这样的东西:
struct Chunk {
int a;
string b;
int c;
};
所以,我想,我不能使用fread和fwrite函数从文件中读写这个结构.因为字符串可能会保留不同的内存容 但这样的代码工作正常.
Chunk var;
fwrite(&var, sizeof(Chunk), 1, file);
fread(&var, sizeof(Chunk), 1, file);
它真的有些问题吗?
假设我有一个中等大小的文本文件(~850kb,10,000+ 行)
我想替换分布在文件中的特定行(或几行)。
当前执行此操作的方法包括重写整个文件。我当前使用的方法是逐行读取整个文件,写入 .tmp 文件,完成后,我将 tmp 文件 rename() 为原始源文件。
它有效,但速度很慢。当然,随着文件的增长,执行时间也会增加。
是否有另一种方法(使用 PHP)来完成工作,而不必每次需要替换或删除一两行时都重写整个文件?
谢谢!我环顾四周,在stackoverflow上找不到答案。
像往常一样,指针的问题.这次我试图读取一个文件(以二进制模式打开)并将其中的一部分存储在std :: string对象中.让我们来看看:
FILE* myfile = fopen("myfile.bin", "rb");
if (myfile != NULL) {
short stringlength = 6;
string mystring;
fseek(myfile , 0, SEEK_SET);
fread((char*)mystring.c_str(), sizeof(char), (size_t)stringlength, myfile);
cout << mystring;
fclose(myfile );
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?我没有得到任何消息.我确定文件没问题当我尝试使用char*它确实有效但我想将它直接存储到字符串中.谢谢你的帮助!
当我尝试使用时读取csv文件时data.table:fread(fn, sep='\t', header=T),它会在此行上显示"不平衡"错误.数据有3个整数变量和1个字符串变量.csv文件中的字符串没有附上",是的有一些包含"在字符串变量中的行和"字符不成对.
我想知道是否有可能fread只是忽略"变量中的未配对并继续读取数据?谢谢.
这是样本数据(只有一条记录)
N_ID VISIT_DATE REQ_URL REQType
175931 2013-3-8 23:40:30 http://aaa.com/rest/api2.do?api=getSetMobileSession&data={"imei":"60893ZTE-CN13cd","appkey":"android_client","content":"Z0JiRA0qPFtWM3BYVltmcx5MWF9ZS0YLdW1ydXoqPycuJS8idXdlY3R0TGBtU 1
Run Code Online (Sandbox Code Playgroud) 我正在用C创建一个归档程序,我想要它保存我提供的文件,列出并提取它们.
我有很多问题,因为我使用文本文件进行保存,如果我想处理音乐或照片等二进制文件,它不是最佳选择,因为当我提取它们时,它们没有正确执行(它们已损坏).为了解决这个问题,我想创建一个二进制存档文件.
文件写入代码(提取时)如下:
void scriviFile(const char * arrivo) //scrive i file creati in precedenza
{
FILE * partenza;
FILE * target;
int c;
int spazio = 'a';
int i = 0;
int pos;
char * path;
path = collegaSlash(getcwd(NULL, 0), nome);
partenza = fopen(path, "rb");
fseek(partenza, inizio, SEEK_SET);
target = fopen(arrivo, "wb"); //apro il file
if (target) { //se è aperto
while ((c = fgetc(partenza)) != EOF && ftell(partenza)<=fine-10) { //e il carattere preso non eccede la fine del file …Run Code Online (Sandbox Code Playgroud) 在我继续我的程序之前,我有一个需要解决的大问题.
我必须打开一个二进制文件,读取它的内容,将内容保存到缓冲区,使用malloc在堆上分配空间,关闭文件,最后是printf(.bin文件的内容).我到目前为止(关闭文件尚未实现):
void executeFile(char *path){
FILE *fp; /*filepointer*/
size_t size; /*filesize*/
unsigned int buffer []; /*buffer*/
fp = fopen(path,"rb"); /*open file*/
fseek(fp, 0, SEEK_END);
size = ftell(fp); /*calc the size needed*/
fseek(fp, 0, SEEK_SET);
buffer = malloc(size); /*allocalte space on heap*/
if (fp == NULL){ /*ERROR detection if file == empty*/
printf("Error: There was an Error reading the file %s \n", path);
exit(1);
}
else if (fread(&buffer, sizeof(unsigned int), size, fp) != size){ /* if count of read bytes != …Run Code Online (Sandbox Code Playgroud) fread一次可以读取的字节数是否有限制?或者我可以读取任何尺寸的我想要充入我的指针?例如,我可以使用 fread 读取一次 50MB 的文件并将其装入字符指针吗?
我有两个二进制文件,我想比较Byte by Byte.我想出了以下代码:
int CompareFiles(char *pFname1, char *pFname2)
{
FILE *pFile1,*pFile2;
long lSize1, lSize2; // file length
int i=0;
char tmp1, tmp2;
pFile1 = fopen(pFname1,"r");
pFile2 = fopen(pFname2,"r");
// obtain file size:
fseek (pFile1 , 0 , SEEK_END);
lSize1 = ftell (pFile1);
rewind (pFile1);
// obtain file size:
fseek (pFile2 , 0 , SEEK_END);
lSize2 = ftell (pFile2);
rewind (pFile2);
if (lSize1 != lSize2) {
printf("File sizes differ, %d vs. %d\n",lSize1,lSize2);
return ( ERROR );
}
for (i=0;i<lSize1;i++) {
fread(&tmp1, …Run Code Online (Sandbox Code Playgroud) 我希望能够读取Windows文本文件,在内存中修改它,然后用修改后的数据覆盖旧文件.但是,fread似乎并不存储我的Windows文本文件中存在的回车符,这会在我写旧数据时丢失.我找不到任何似乎有这个问题的人.
以下是一些演示此问题的示例代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* textFile;
long fileSize;
char fileCharacterBuffer[100];
int i;
for(i = 0; i < 100; i++)
{
fileCharacterBuffer[i] = '\0';
}
textFile = fopen("./Test.txt", "r+");
fseek(textFile, 0L, SEEK_END);
fileSize = ftell(textFile);
fseek(textFile, 0L, SEEK_SET);
fread(fileCharacterBuffer, 1, fileSize, textFile);
}
Run Code Online (Sandbox Code Playgroud)
测试文件:
3
112
REd
110
green
#5/09/2014
5087 - 5/6/2014
Run Code Online (Sandbox Code Playgroud)
它的十六进制转储显示其EOL是\ r \n:
Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 33 0D …Run Code Online (Sandbox Code Playgroud) 我如何制作一个脚本可以告诉我当它存储在 composer.json 时我运行的是哪个版本?
作曲家.json
{
"require": {
"someLiberyNameHere": "8.3.3.1"
}
}
Run Code Online (Sandbox Code Playgroud)