Tag: 输出

printf与scanf请求输入。 BST循环错误

我一直试图看看为什么printf在打印文件输入后没有打破循环。 .c文件是BST,我现在正在测试是否已经构建了一个树,但似乎无法退出printf循环。 我需要printf循环来获得代码的正确输出。 有关此错误发生原因的任何建议。 显示完整的代码和输出。 #include “bst.h” #include #include ///Author: Joe W //arbitrary list of temp nodes TreeNode *new_node, *root, *tmp, *parent; int elemArray[100], i1, i2, i0; /* Insert a new node into the tree by referencing the root and using recursion */ TreeNode* getN(int dataElem) { TreeNode* temp; temp = malloc(sizeof(TreeNode*)); temp-> left = […]

为什么fgets没有输入第一个值?

我正在编写一个程序来快速编写我的html文件。 当我来写我的页面内容时,我遇到了问题。 #include int main() { int track; int question_no; printf(“\nHow many questions?\t”); scanf(“%d”,&question_no); char question[question_no][100]; for(track=1;track<=question_no;track++) { printf("\n%d. “,track); printf(“\nQuestion number %d.\t”,track); fgets(question[track-1],sizeof(question[track-1]),stdin); printf(“\n\n\tQ%d. %s “,track,question[track-1]); } } 在这个程序中,我正在写一些问题及其答案(在html文件中)。 当我测试运行这个程序时,我将question_no的值输入到3.但是当我输入我的第一个问题时,它没有question[0] ,因此第一个问题没有输出。 其余的问题输入没有问题。 我在stackoverflow上搜索了一些问题,发现fgets()查找最后的\0字符,并且\0停止它。 我还发现我应该使用缓冲区通过fgets()输入,所以我使用: setvbuf和setbuf但也没有用(我可能编码错了)。 在我的第一个和最后一个(以及) scanf语句之后我也使用了fflush(stdin)从stdin中删除任何\0字符,但这也没有用。 有没有办法接受fgets()的第一个输入? 我现在正在使用stdin和stdout。 我不访问,读取或写入任何文件。

如何使我的输出仅显示我的检查生成器程序所需的内容

我已经制作了一个程序,可以输出输入的任何数字,但它会将其转换为单词而不仅仅是数字,例如,如果你输入“1234.56”,它将把它转换为“一千二百三十四美元和… .56分“。 分数应始终为数字。 到目前为止,一切都很好,但如果我投入的金额少于一千,我会在那里得到诸如“千”或“百”等多余的词。 例如,如果我输入“15.77”,我的输出将是“千美元十五美元和…… 77美分”。 我不希望千人或者百人在那里,没有那些将是完美的! 代码如下: #include void printNum(int); void printNum2(int); void printNum3(int); int main() { int a = 0; int b = 0; int c = 0; int d = 0; int num = 0; int printcents; //To convert the float “cents” to an integer. float inclusive; float cents; printf(“Welcome to the […]

你如何写入C中的txt文件的特定行?

我想覆盖txt文件中的某一行,同时保持所有其他行相同。 有这么好的简单方法吗?

写入c MPI中的输出文件

我正在研究这个MPI代码,一切都正常工作,但我无法将程序的输出写入文件。 这里有一些代码来说明我的问题 int main(int argc, char *argv[]){ FILE *filename; int size, my_rank; int count =0; int tag =99; int limit = 5; MPI_Init(&argc, &argv); MPI_Status status; MPI_Comm_size(MPI_COMM_WORLD,&size); MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); if(my_rank ==0) printf(“Process %d started the game and initialized the counter\n\n”,my_rank); MPI_Barrier(MPI_COMM_WORLD); if (size != 2) {//abort if the number of processes is not 2. fprintf(stderr, “only […]

C编程输出文本文件

嗨,我刚开始编程并有一个初学者问题:我想更好地了解fprint()函数是如何工作的,因为有时当我用它创建文本文件时,我意识到有各种类型的文件,例如。(只读,附加)和写)。 当我想写一个我用循环创建的文件时,添加内容的顺序似乎在我做的时候会改变 file = fopen(name,”a+”); 如果是的话,我无法在循环中添加所有内容 file = fopen(name,”w”); 那么创建文本文件最方便的方法是什么? 谢谢! 所以说我想写一个trie树中的所有单词,文本文件中的顺序不同于用print()替换fprint()我有一个树的全局节点和一个指向它的节点指针function struct node *root = (struct node *)malloc(sizeof(struct node)); function是: void printResult(struct node* r){ struct node *p = r; FILE *file; sprintf(name, “man%d.txt”, num); file = fopen(name,”a+”); int i=0; int temp; while(i!=26){ if(p->child[i]==NULL){ i++; continue;} if(p->child[i]->isword==1&&p->child[i]->leaf==1){ word[k]=i+’a’; word[k+1]=’\0′; fprintf(file,”%s”, word);fprintf(file,”%s”,” ” ); fprintf(file,”%d”, p->child[i]->occurrence);fprintf(file,”%s”,” […]

格式字符串scanf()中的空格

#include int main() { int a,b; printf(“Enter values of a and b\n”); scanf(” %d%d “,&a,&b); printf(“a=%db=%d”, a, b); return 0 ; } 这里如果我使用scanf(),就像在我的代码中那么编译器期望用户输入三个值,我无法理解这一点,当我使用scanf()而没有任何空格时,它只按要求请求两个输入,所以我很困惑这两者之间的区别,PLZ解释……

为什么此代码中的缓冲区溢出与我的预期不同?

我有这个程序: #include #include #include void main(void) { char *buffer1 = malloc(sizeof(char)); char *buffer2 = malloc(sizeof(char)); strcpy(buffer2, “AA”); printf(“before: buffer1 %s\n”, buffer1); printf(“before: buffer2 %s\n”, buffer2); printf(“address, buffer1 %p\n”, &buffer1); printf(“address, buffer2 %p\n”, &buffer2); strcpy(buffer1, “BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB”); printf(“after: buffer1 %s\n”, buffer1); printf(“after: buffer2 %s\n”, buffer2); } 哪个印刷品: before: buffer1 before: buffer2 AA address, buffer1 0x7ffc700460d8 address, buffer2 […]

该函数输出的说明

我正在做复习问题,问我“以下是什么输出”,而我在理解这个function方面遇到了一些麻烦: int a = 1, b = 1, c = -1; c = –a && b++; printf(“%d %d %d”, a, b, c); 输出是010.我的问题是关于第2行, c = –a && b++ 。 如何处理此行,以及它如何工作/更改值? 如果它是c = –a || b++ c = –a || b++ ? 从我的理解,我认为输出将是020。

VB换行逃脱char?

在CI中使用”1st line 1\n2nd line”作为换行符,但是VB怎么样? 我知道”1st line” & VbCrLf & “2nd line”但它太冗长,VB中换行符的转义字符是什么? 我想要打印 1st line 2nd line 我尝试使用\n但无论运行编译器多少次,它总是会输出 1st line\n2nd line 有任何想法吗?