gpt4 book ai didi

c - fscanf 不能正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:04:32 24 4
gpt4 key购买 nike

您好,有一个关于多进程编程的作业,当我尝试逐行读取字符时遇到了一个问题(每行由 3 个字符和 1 个整数组成)。问题是当我使用 fscanf 时它不能正常工作并且它每 2 个步骤就成功读取字符。在这里你可以看到我的代码;

#include <stdio.h>     /* basic I/O routines.   */
#include <stdlib.h>
#include <unistd.h> /* define fork(), etc. */
#include <sys/types.h> /* define pid_t, etc. */
#include <sys/wait.h> /* define wait(), etc. */
#include <signal.h> /* define signal(), etc. */
#include <pthread.h>
#include <ctype.h>

void child_process(int);
void parent_process();
void function();
int counter=0;
int bond_number=0;

char* f_strand;

int main(int argc, char* argv[]) {

counter = atoi(argv[1]);

function();

fflush(stdout);

execl("/usr/bin/killall","killall","tail",(char *) 0);
return 0;
}

void function(){

int i,k;
pid_t child_pid[counter];
char array[counter];
char array_opst[counter];
srand ( time(NULL) );
int temp;

FILE* fptr;
fptr = fopen("sample.txt","w");

if(! (f_strand=(char*)malloc(counter*sizeof(char))) ){
printf("Error\n");
exit(1);
}

for(i=0; i<counter; i++){

temp = rand()%4;

if(temp==0){
*(f_strand+i) = 'A';
fprintf(fptr,"A\n");
}
else if(temp==1){
*(f_strand+i) = 'C';
fprintf(fptr,"C\n");
}
else if(temp==2){
*(f_strand+i) = 'T';
fprintf(fptr,"T\n");
}
else if(temp==3){
*(f_strand+i) = 'G';
fprintf(fptr,"G\n");
}
}

fclose(fptr);

for (i = 0; i < counter; i++) {
if ( (child_pid[i] = fork() ) < 0) {

perror("fork");
abort();
}
else if (child_pid[i] == 0) {

child_process(i);
exit(0);
}
}
int status;
pid_t pid;
int num=counter;
while (num > 0) {
pid = wait(&status);
//printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
--num; // Remove pid from the pids array.
}


parent_process();
printf("\n");

}

void child_process(int index){

int i;
char str,str1,str2;

FILE* fptr;
fptr = fopen("sample.txt","r");

fseek (fptr, 2*index, SEEK_SET);
fscanf(fptr,"%c", &str);

FILE* fptr2;
fptr2 = fopen("sample_2.txt","a");

if( str == 'A' ){
str1='T';
str2=';';
fprintf(fptr2,"%c%c%c%d\n",str,str2,str1,index);
}
else if( str == 'T' ){
str1='A';
str2=';';
fprintf(fptr2,"%c%c%c%d\n",str,str2,str1,index);
}
else if( str == 'G' ){
str1='C';
str2=':';
fprintf(fptr2,"%c%c%c%d\n",str,str2,str1,index);
}
else if( str == 'C' ){
str1='G';
str2=':';
fprintf(fptr2,"%c%c%c%d\n",str,str2,str1,index);
}

fflush(stdout);
fclose(fptr);
fclose(fptr2);
sleep(1);

}

void parent_process(void){

FILE* fptr;
fptr = fopen("sample_2.txt","r");

char str,str1,str2;
int index,i,k;
char array_opst[counter]; // second strand
char array_type[counter]; // bond type

i=0;

while( !feof(fptr) ){

if(4==fscanf(fptr,"%c%c%c%d",&str,&str1,&str2,&index)){

if(feof(fptr))
break;

printf("STR = %c\n, K = %d",str,k);

array_type[index]=str1;
array_opst[index]=str2;
printf("WHILE\n");

if(array_type[index] == ':')
bond_number=bond_number+2;
else if(array_type[index] == ';')
bond_number=bond_number+3;
}

i++;
}
fclose(fptr);

}

这是sample2.txt的输出

C:G5
G:C6
A;T4
C:G3
G:C7
C:G8
G:C2
A;T9
T;A1
C:G0

parent_process() 中,它尝试从 sample2.txt 文件中逐行读取 3 个字符和一个整数。但它每两行执行一次此过程。我的意思是它读取前三五行并继续。如果您能提供帮助,我将不胜感激。无论如何,我将不胜感激。

最佳答案

这是一个有趣的多方面问题。首先要注意的是,您正在使用 c转换。 C99 标准规定如下 (§7.19.6.2¶8)。

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier.

现在,让我们看一下重要的一行。

    if(4==fscanf(fptr,"%c%c%c%d",&str,&str1,&str2,&index)){

发生的是 fscanf 占用了空格(条目之间的填充、行终止符等)。 .发生这种情况时,读取未对齐,因此在遇到 d 时无法读取整数。转换。结果,fscanf不计算为 4 , 不采用分支,因此不打印错误读取的数据。

相反,它需要错误地读取每隔一行以“重新对齐”到正确的输入,因此您只能看到打印的这些选择行。为了证明这一点,请尝试仅用 0 < fscanf(...) 替换条件不正确的读取将产生输出以演示读取每隔一行的中间尝试。

关于c - fscanf 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12875122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com