我正在尝试用C语言读取文件
while(1){
if(feof(file)){
break;
}
printf("%c",fgetc(file));
}
Run Code Online (Sandbox Code Playgroud)
最后我得到了特殊的角色,比如 我在文件中没有这样的东西
我在从二进制文件复制和写入文本文件时遇到问题。我编写了一个能够从文本文件复制并写入二进制文件的程序,但我不能反过来。
这是我遇到问题的功能:
void CopyBin2Text(char* rafname, char* txtname)
{
FILE * fraf = fopen(rafname,"rb");
FILE * ftxt = fopen(txtname,"r+");
//READ FROM BINARY FILE
struct PERSON p;
int ByteOfBin;
printf("ID \t NAME \t\t BALANCE \n");
printf("---------------------------------------\n");
Run Code Online (Sandbox Code Playgroud)
当我运行我的程序时,它在打印上述语句后停止在这里
while(!feof(fraf))
{
fscanf(fraf, "%d %s %f", &p.ID, p.name, &p.balance);
ByteOfBin = ((p.ID/10-1)*sizeof(p));
fseek(ftxt,ByteOfBin, SEEK_SET);
fwrite((char *)&p, sizeof(p), 1, ftxt);
}
fclose(fraf);
fclose(ftxt);
}
Run Code Online (Sandbox Code Playgroud)
我注意到的另一个问题是文本文件变得太大而无法打开。结果是我必须删除文本文件并重新创建它。任何人都可以解释导致这种情况发生的原因吗?
我有一个带有cmd命令转发输出的文件.AC程序循环遍历字符,直到它到达文件末尾.但不幸的是,在文件实际结束之前,文件中有一些EOF指示符.该文件的内容是:
Abbildname PID Sitzungsname Sitz.-Nr. Speichernutzung
========================= ======== ================ =========== ===============
System Idle Process 0 Services 0 8 K
System 4 Services 0 5ÿ744 K
smss.exe 440 Services 0 276 K
csrss.exe 616 Services 0 1ÿ924 K
wininit.exe 720 Services 0 944 K
services.exe 792 Services 0 7ÿ892 K
lsass.exe 800 Services 0 14ÿ300 K
svchost.exe 924 Services 0 420 K
fontdrvhost.exe 952 Services 0 680 K
svchost.exe 960 Services 0 23ÿ880 K
WUDFHost.exe 1012 Services 0 7ÿ416 K
svchost.exe …Run Code Online (Sandbox Code Playgroud) 我不明白为什么我的下面的脚本似乎可以在不创建任何文件的情况下工作。
脚本.c:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]){
printf("P_tmpdir is '%s'\n", P_tmpdir);
FILE *tmp = tmpfile();
if(tmp == NULL){
printf("Unable to create temp file");
exit(1);
}
else{
printf("Temporary file is created\n");
}
for(int i = 0; string[i] != '\0'){
fputc(string[i], tmp);
}
rewind(tmp);
while(!feof(tmp)){
putchar(fgetc(tmp));
}
sleep(3);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
P_tmpdir 变量返回给我“/tmp”目录,尽管在睡眠期间没有在其中创建新文件...您能帮助我或解释一下吗?
我在C编程,我开始在文件中做一些像wordcount这样的基本编程,但不幸的是我的程序执行中存在缺陷.gcc编译器显示这样一个警告:
test.c: In function ‘main’:
test.c:11: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast
/usr/include/stdio.h:269: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
Run Code Online (Sandbox Code Playgroud)
第11行是if语句的行
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define FAIL -1
#define SUCCESS 1
int main (char *filename) {
FILE *fp;
char c;
int wordcount = 0;
if ((fp = fopen(*filename,"r")) == NULL)
return FAIL;
while (!feof(fp))
{
while(!isalpha(fgetc(fp)))
{
wordcount++;
}
}
printf("wordcount: %d",wordcount);
fclose(fp);
return SUCCESS;
}
Run Code Online (Sandbox Code Playgroud) 我正在研究C源代码并获得它的输入
while(!feof(stdin)){
fscanf(stdin, "%d ...
Run Code Online (Sandbox Code Playgroud)
我无法理解的是你如何让它停止输入并处理输入?Ctr + Z只是杀死了整个过程,我尝试按下输入一堆没有输入,它不起作用.
我的文本文件包含
Wew213
Wew214
Wew215
Run Code Online (Sandbox Code Playgroud)
我在程序中的输入是
Wew213
Run Code Online (Sandbox Code Playgroud)
但它告诉我输出
"Not Matched"
Run Code Online (Sandbox Code Playgroud)
实际上我正在做的是我想输入输入,如果输入匹配文本文件中的数字,它应该通过if语句运行输出否则语句
这是我的计划
char file_data[10];
std::ifstream file_read ("D:\\myfile.txt");
cout<<"Enter the number to search"<<endl;
char val[10];
cin>>val;
while(!file_read.eof())
{
file_read>>file_data;
cout<<file_data<<endl;
}
if (val == file_data)
{
cout<<"Matched"<<endl;
}
else
{
cout<<"Not Matched"<<endl;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过输入到标准输入的字符串选择性地过滤文本文件.
我想知道为什么以下代码不起作用以及如何解决它:
void get_filtered_list()
{
FILE * f;
f=fopen("presentlist.txt","r");
printf("Enter the city by which you want to select lines:\n");
char stringToFind[20];
fgets(stringToFind, sizeof(stringToFind), stdin );
char line[160];
while (!feof(f)){
fgets(line,sizeof(line),f);
if(strstr(line, stringToFind) != NULL)
{
printf("%s",line);
}
}
fclose(f);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码试图获取一个文本文件,打开该文件,然后逐行读取文件,并为执行该strstr()函数的每一行以文件的当前行作为参数1作为字符串,并给出给定的名称城市作为参数2作为字符串.
然而,我得到的结果是打印文件的整个内容(最后一行打印两次,虽然这是一个单独的问题,我知道这部分的修复).
我正在阅读的C书说明该strstr()函数用于在"haystack"字符串中查找"needle"字符串,因此它是C++ substr()函数的C等价物.
strstr() 将参数1作为干草堆,将参数2作为针.
我首先从标准输入读入到针中,然后逐行检查是否strstr()返回NULL(如果在haystack中找不到针,它应该返回NULL)并且如果它返回NULL之外的其他东西,则意味着它找到字符串中的子字符串,它应该只打印行THEN.
而是打印文件中的所有行.为什么?
如果我将其切换为f(strstr(line, stringToFind))相反,那么它绝对不打印.
为什么?
我的程序应该读取一些段来标识一个平面.每行3个段.输入档案是:
3 4 25 -4 -30 2 6 7 9 10 3 4
3 4 4 -4 -3 2 6 7 9 10 5 6
Run Code Online (Sandbox Code Playgroud)
它被读作坐标:(3,4)(25,-4)( - 30,2)(6,7)(9,10)(3,4)
Segment将是一对坐标:S01 - (3,4)(25,-4),依此类推
代码:
typedef struct{
int x1, x2;
int y1, y2;
int id;
}Segment;
int main(){
FILE *file;
int i=0, j=0;
Segment *seg;
seg=(Segment*)malloc(500*sizeof(Segment));
file = fopen("input.txt", "r");
while(!feof(file)){
for(i=0; i<3; i++){
fscanf(file, "%d %d %d %d", &seg[j].x1, &seg[j].y1, &seg[j].x2, &seg[j].y2);
seg[j].id=i+1;
printf("%d %d %d %d - ID: %d\n", seg[j].x1, seg[j].y1, …Run Code Online (Sandbox Code Playgroud) 我的readfile方法有问题.我无法在文本文件中打印出数字值,但我的名字很好.
这就是我的文本文件中的所有内容:
Bob
10
12.00
Run Code Online (Sandbox Code Playgroud)
码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "iofunctions.h"
int readfile( struct account accarray[], int* numcust, char filename[])
{
/* variables */
FILE *inputFile;
char line[25];
char spaceEater[25];
char name[25]={"\0"};
int accountNum;
float money ;
inputFile = fopen("People_Info.txt", "r");
if(inputFile == NULL)
{
fprintf(stderr, "Sorry, I cannot open this file. Also it's empty.\n");
exit(1);
}
/*outer loop is to check if file is not null*/
while(!feof(inputFile))
{
/*while file has something*/
while(fscanf(inputFile,"%s %d …Run Code Online (Sandbox Code Playgroud) c ×9
file ×4
batch-file ×1
binary ×1
binaryfiles ×1
c++ ×1
cmd ×1
feof ×1
file-io ×1
linux ×1
pointers ×1
stdio ×1
stream ×1
string ×1
text-files ×1