我有一个与while循环相关的奇怪问题.我有一个函数,它在process(print_file())的父元素的末尾被调用,并且它不接受真正的条件继续.这是我简单的多进程代码,如下所示.
#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 <time.h>
#include <ctype.h>
void print_file(char* [], char* []);
void child_process(int,int);
void parent_process();
int counter=0;
int main(int argc, char* argv[]) {
counter = atoi(argv[1]);
int i,k;
pid_t child_pid;
int child_status;
char* array[counter];
srand ( time(NULL) ); …Run Code Online (Sandbox Code Playgroud) 我在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只是杀死了整个过程,我尝试按下输入一堆没有输入,它不起作用.
我期待找到C fgets的C++ fstream等效函数.我尝试使用fstream的get函数,但没有得到我想要的.get函数不提取delim字符,而fgets函数用于提取它.所以,我写了一个代码来从我的代码本身插入这个delim字符.但它给出了奇怪的行为.请参阅下面的示例代码;
#include <stdio.h>
#include <fstream>
#include <iostream>
int main(int argc, char **argv)
{
char str[256];
int len = 10;
std::cout << "Using C fgets function" << std::endl;
FILE * file = fopen("C:\\cpp\\write.txt", "r");
if(file == NULL){
std::cout << " Error opening file" << std::endl;
}
int count = 0;
while(!feof(file)){
char *result = fgets(str, len, file);
std::cout << result << std::endl ;
count++;
}
std::cout << "\nCount = " << count << std::endl;
fclose(file);
std::fstream fp("C:\\cpp\\write.txt", std::ios_base::in);
int …Run Code Online (Sandbox Code Playgroud) 我有这个代码
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv) {
ifstream in;
in.open(*(argv+1));
char c;
while (true) {
if (in.eof())
break;
c = in.get();
cout << c;
}
in.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我读取了通过命令行传递的文件作为第一个参数.在阅读文件时,我得到了char'?' 作为最后一个.为什么?
如果我删除此行
in.open(*(argv+1));
Run Code Online (Sandbox Code Playgroud)
我只会得到char'?'.
我的程序应该读取一些段来标识一个平面.每行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) 我在Windows 8.1中使用MinGW,并且我有一个原始数字的输入文本文件(每行一个),我想将它们以二进制形式写入新的二进制文件中。该示例在编译时不会出现以下问题:
gcc -pedantic -Os -c my_code.c -o my_code.exe
Run Code Online (Sandbox Code Playgroud)
但输出是
$ my_code.exe
sh: ./my_code.exe: Bad file number
Run Code Online (Sandbox Code Playgroud)
这是我写的代码:
#include<stdio.h>
int main ()
{
FILE *fp;
FILE *prob;
int length;
char buffer[30];
// Open file containing numbers
if ((prob = fopen("raw_numbers.txt","r")) == NULL)
{
printf("Could not open raw_numbers.txt\n");
exit(1);
}
/* Create output binary file */
fp = fopen( "file.bin" , "w" );
while( ! feof(prob) )
{
fgets(buffer, sizeof(buffer), prob);
fwrite((const void*) & buffer, 1, sizeof(buffer), fp);
}
fclose(prob);
fclose(fp); …Run Code Online (Sandbox Code Playgroud) 我的理解是fgets()返回输入的字符串参数,但是,
我收到"错误:从类型'char*'"分配类型'char [101]'时出现不兼容的类型,
那么,为什么变量'line'被认为是'char [101]'而不是'char*'?
char line[101] = "";
while (feof(filePtr) == 0){
line = fgets(line, 101, filePtr);
strcpy(strPtr, line);
}
Run Code Online (Sandbox Code Playgroud) 我练习用 C 语言处理文件。我需要读取一个记录了大约 9000000 个数据的文件。当我读取前 9000 条数据时它起作用了。但是,当我设置 NUM_DATA = 9000000 时,它总是崩溃。我该如何解决这个问题?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_LENGTH 10
#define NUM_DATA 9000000
int main(int argc, char **argv){
char **signal = malloc( NUM_DATA * MAX_LENGTH * sizeof(char *) );
char **svid = malloc( NUM_DATA * MAX_LENGTH * sizeof(char *) );
char GetString[110];
int num;
double tw[NUM_DATA], ip[NUM_DATA], qp[NUM_DATA]
char a[20], b[20], c[20], d[20], e[20], f[20], g[20], h[20], z[20], x[20], w[20];
FILE *fIn;
fIn = fopen(argv[1], "r");
// read …Run Code Online (Sandbox Code Playgroud)