我正在尝试从C中的二进制文件中编写和读取链接列表.我的目的是为护理之家保存和加载常驻数据(实际上,我是护士),以便通过资源利用率对每个居民进行分类组.我已经使用一系列结构为固定数量的居民(32,即设施的容量)做了这件事,但现在我需要为一组可变的居民做这件事,以便进行统计研究.显然,对于第一次尝试,我将结构简化为最小,因为实际结构包含109个数据.
我非常接近解决方案,但有些东西不起作用,也就是说,保存列表的每个元素都与一个空格一起交替.这个代码应该读取二进制文件中的列表,在终端上显示它,添加一个新元素,保存列表.当然,每个程序都应该放在一个函数中.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
struct pat
{
char surn [16];
char name [16];
struct pat *next;
};
static FILE *h;
static struct pat *osp;
static struct pat *first;
struct pat *make_structure (void);
int main()
{
initscr();
raw();
keypad (stdscr, TRUE);
noecho();
clear();
osp=make_structure();
first=osp;
h=fopen ("archivio","r");
if (h==NULL)
printw ("Archivio inesistente\n");
else
{
while (!feof(h))
{
printw ("Lungh. nome = %d\n",sizeof osp->name);
fread (osp->surn,sizeof osp->surn,1,h);
fread (osp->name,sizeof osp->name,1,h);
printw ("Cognome: %s\tNome: %s\n",osp->surn,osp->name);
osp->next=make_structure();
osp=osp->next; …Run Code Online (Sandbox Code Playgroud) 我试图了解何时clearerr()应使用stdio函数。
例如,如果我fread()或fwrite()某人有效FILE*并且得到了一个短计数并且ferror为真,我该怎么办?
从我至今读,fread()并且fwrite()功能强大且将阻止和/或重试(如果有锁和/或可能在较低水平的功能发生中断),因而似乎永远不会任何点使用clearerr(),因为fread或fwrite错误会被如此灾难性的,试图恢复毫无意义。
此外,ferror()仅告诉我有错误,而不是错误。
#define SZ 1024
FILE* fp = fopen( "foo", "r" );
if ( fp ) {
char b[SZ];
int ch_count = fread( b, sizeof(char), SZ, fp );
if ( ch_count != SZ && ferror( fp ) ) {
// how would clearerr() be used. I don't know?
// ....
// should I drop through here …Run Code Online (Sandbox Code Playgroud) 我就是这样做的,但我不确定这是首选的习语:
FILE *fp = fopen(argv[0], "r");
// handle fopen() returning NULL
while (!feof(fp)) {
char buffer[80]; // statically allocated, may replace this later with some more sophisticated approach
int num_chars = 0;
for (int ch = fgetc(fp); ch != EOF && ch != '\n'; ch = fgetc()) {
buffer[num_chars++] = ch;
}
// null-terminate the string
buffer[num_chars] = '\0';
printf("%s\n", buffer);
}
Run Code Online (Sandbox Code Playgroud)
这没关系,有什么建议可以改善吗?
我正在尝试读取文本文件并将数据存储在矩阵中.我在读完每一行后打印结果,似乎它正在工作,但如果我在最后打印矩阵,我的结果不一样.我找不到我做错了什么!
int main()
{
int i,j;
double value[10000][2];
FILE *archivo;
archivo = fopen("pruebas.txt","r");
if (archivo == NULL)
exit(1);
i=0;
while (feof(archivo) == 0)
{
fscanf( archivo, "%lf %lf %lf", &value[i][0],&value[i][1],&value[i][2]);
printf("%10.0f %f %f\n", value[i][0], value[i][1], value[i][2]);
i++;
}
printf("Your matrix:\n");
for(j = 0; j < i; j++)
printf("%10.0f %10.3f %10.3f\n", value[j][0], value[j][1], value[j][2]);
fclose(archivo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是该程序的输出:
1 2 3
4 5 6
7 8 9
Your matrix:
1 2 4
4 5 7
7 8 9
Run Code Online (Sandbox Code Playgroud) 关于以下功能,完成此功能后,调试器会向我显示__stack_chk_fail。
我的系统是Mac OS。
这是因为我的堆栈通过检查引用而自身溢出。
同样基于我的实验,如果设置vocab_size = 30000,则显示__stack_chk_fail错误,但是当vocab_size = 20000时,则可以。
所以我相信
vocab = (struct vocab_word *)malloc ((size_t) ((vocab_size + 1) * sizeof(struct vocab_word)));
Run Code Online (Sandbox Code Playgroud)
是问题。但是malloc在堆而不是堆栈上分配内存,所以我想知道哪里出错了?
void populate_vocab(){
FILE *fin;
fin = fopen(word_file, "rb");
vocab = (struct vocab_word *)malloc ((size_t) ((vocab_size + 1) * sizeof(struct vocab_word)));
char word[MAX_STRING];
int word_idx = 0;
int num = 0;
boolean word_mode = 1;
long long cur_vocab_size = 0;
while (!feof(fin)) {
ch = fgetc(fin);
if(ch == ' '){
word_mode = 0;
}else if(ch == '\n'){
word_mode …Run Code Online (Sandbox Code Playgroud) 最近我正在阅读 Deitel 的《C How to Program》第 7 版的文件处理部分。为了写入文件,它使用以下示例:
// Fig. 11.2: fig11_02.c
// Creating a sequential file
#include <stdio.h>
int main( void )
{
unsigned int account; // account number
char name[ 30 ]; // account name
double balance; // account balance
FILE *cfPtr; // cfPtr = clients.dat file pointer
// fopen opens file. Exit program if unable to create file
if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) {
puts( "File could not be …Run Code Online (Sandbox Code Playgroud) 该程序应该将文件的内容从客户端发送到服务器端的输出文件。但是,我的代码适用于少数文件,不适用于大多数文件。例如,如果我尝试将调用的文件的内容复制morefood.txt到输出文件 say picolo.txt,则不会复制任何内容。
服务器代码:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int main(int argc, char *argv[]){
int fd =0, confd = 0;
struct sockaddr_in serv_addr;
char buff[1025];
int num;
fd = socket(AF_INET, SOCK_STREAM, 0);
printf("Socket created\n");
memset(&serv_addr, '0', sizeof(serv_addr));
memset(buff, '0', sizeof(buff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(fd, 10);
FILE* fp = fopen( "picolo.txt", "wb");
if(fp == NULL){ …Run Code Online (Sandbox Code Playgroud) 假定交换文件中的每两行,直到仅剩一行或所有行都用完为止。我不想使用其他文件。
这是我的代码:
#include <stdio.h>
int main() {
FILE *fp = fopen("this.txt", "r+");
int i = 0;
char line1[100], line2[100];
fpos_t pos;
fgetpos(fp, &pos);
//to get the total line count
while (!feof(fp)) {
fgets(line1, 100, fp);
i++;
}
i /= 2; //no. of times to run the loop
rewind(fp);
while (i-- > 0) { //trying to use !feof(fp) condition to break the loop results in an infinite loop
fgets(line1, 100, fp);
fgets(line2, 100, fp);
fsetpos(fp, &pos);
fputs(line2, fp);
fputs(line1, fp);
fgetpos(fp, …Run Code Online (Sandbox Code Playgroud) 我的问题很简单,但我无法在谷歌上找到它,所以我在这里.
基本上,作为一个例子,我正在读取一堆整数,直到输入文件中的EOF.我使用fgetc来检查,但随后它将文件指针移动到第二个整数的地址.如何检查此while循环中的EOF而不检查文件指针?
请记住,我将使用此循环执行比使用整数扫描更复杂的操作.另外,我不知道我使用fscanf而不是fgets.它只是一个简单的例子,向您展示我的意思.
while(fgetc(ifp) != EOF)
{
fscanf(ifp, "%d", &test);
printf("%d\n", test);
}
Run Code Online (Sandbox Code Playgroud)
例如,如果输入文件有整数1-10,则上面的代码将打印:
2 3 4 5 6 7 8 9 10
错过了1!
我不擅长C而且我想做一些简单的事情.我想打开一个二进制文件,读取1024字节数据的块并转储到缓冲区,处理缓冲区,读取另外1024个数据的数据并继续这样做直到EOF.我知道如何/我想用缓冲区做什么,但它是循环部分和文件I/OI一直卡在上面.
PSEUDO代码:
FILE *file;
unsigned char * buffer[1024];
fopen(myfile, "rb");
while (!EOF)
{
fread(buffer, 1024);
//do my processing with buffer;
//read next 1024 bytes in file, etc.... until end
}
Run Code Online (Sandbox Code Playgroud)