本文介绍了帮助一个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好! 我正在尝试从我的C书中做一个练习,恰好是非常类似于最近的一本书。 ;程序"线程。 任务:编写一个程序,使用重定向接受来自磁盘的输入 文件, 计算数量乘以字母表的前三个字母(a,b, c,以及A,B,C) 出现在文件中。 以下是我的尝试: ================================= ========= #include< stdio.h> #include< stdlib.h> #定义MAX 1000 / *文件中的最大字符数* / int main(无效) { char * ptr; int count, ctra = 0, ctrb = 0, ctrc = 0; ptr = malloc(1000 * sizeof(char)); 得到(ptr); for(count = 0; count< MAX; count ++){ if(ptr [count] ==''a''|| ptr [count] ==''A'') ctra ++; if(ptr [count] ==''b''|| ptr [count] ==''B'') ctrb ++; if(ptr [count] == 'c''|| ptr [count] ==''C'') ctrc ++; } printf(" ctra =%d \ n",ctra); printf(" ctrb =%d \ n",ctrb); printf(" ctrc =% d \ n",ctrc); 免费(ptr); 返回0; } ========================================== 问题:只有当已知(b 输入.txt )中的最大字符数(小于1000)时才能正常工作。 我试过包括: ======================= while((ch = getchar())!= EOF){ ctr ++; } ======= ================= 这样做的原因是我可以在malloc中使用ctr来 分配足够的内存来获取文件中的所有字符。 ie。 ptr = malloc(ctr * sizeof(char)); 得到(ptr); 但是,因为getchar *得到了来自stdin的所有字符,声明 得到(ptr)从stdin获得*没有*。 我已经为此工作了2天。它正在推动我NUTTTSSSSS !!! 有人可以给我一个提示或2吗? 先谢谢你们!热爱你的工作! 巴克。 - 使用M2,Opera的革命性e-邮件客户端: http://www.opera.com/m2/Hi guys!I am trying to do an excercise from my C book, which coincidently is verysimilar to the one in the recent "program" thread.Task: Write a program that uses redirection to accept input from a diskfile,counts the number of times the first three letters of the alphabet (a, b,c, and A, B, C)occur in the file.Below is my attempt:==========================================#include <stdio.h>#include <stdlib.h>#define MAX 1000/*Maximum number of characters in the file */int main( void){char *ptr;int count,ctra = 0,ctrb = 0,ctrc = 0;ptr = malloc(1000 * sizeof(char));gets(ptr);for (count = 0; count < MAX; count++) {if( ptr[count] == ''a'' || ptr[count] == ''A'')ctra++;if( ptr[count] == ''b'' || ptr[count] == ''B'')ctrb++;if( ptr[count] == ''c'' || ptr[count] == ''C'')ctrc++;}printf("ctra = %d\n", ctra);printf("ctrb = %d\n", ctrb);printf("ctrc = %d\n", ctrc);free(ptr);return 0;}==========================================Problem: This works fine only if the maximum number of characters ininput.txtis known(less than 1000).I''ve tried including:=======================while ((ch = getchar()) != EOF) {ctr++;}========================The reason for this is so that I can use ctr in a malloc toallocate enough memory to get all the characters in the file.ie. ptr = malloc(ctr * sizeof(char));gets(ptr);However, because getchar *got* all the characters from stdin, the statementgets(ptr) gets *nothing* from stdin.I''ve been working on this for 2 days. It''s driving me NUTTTSSSSS!!!Can someone please give me a hint or 2?Thanks in advance guys! Love your work!Buck.--Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/推荐答案 我想这是因为获取()! [..] - Vijay Kumar R Zanvar 我的主页 - http://www.geocities.com/vijoeyz/ 这里有一些值得深思的东西: 1 )为什么不一次一个地获得每个炭。这可能不会很快,但是它可以工作。 2)如果你想一次加载一个块,请使用fread() 3)如果你想立即加载完整的文件,那么看看fseek() 和ftell()来获取文件大小。我相信这对于非二进制文件来说只是可靠的便携式,但我可能会误会。 HTH AllanHere is some food for thought:1) why dont you get each char, one at a time. This may not be fast, but itwill work.2) If you want to load in a block at a time, use fread()3) If you want to load the complete file in at once, then look at fseek()and ftell() to get the file size. I believe that this is only reliablyportable for non-binary files, but I could be mistaken.HTHAllan 见以上。 - Martin AmbuhlSee above.--Martin Ambuhl 这篇关于帮助一个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!