Tag: c

从C中的结构返回值的函数

我在C语言中有这个结构。 struct webit{ char indice[10]; int prid; double latitude; char date[20]; double longitude; int xspeed; int rEvt; int alti; int seq1; int seq2; int presi; char cod1[5]; char cod2[5]; int iint1; int iint2; int x15; }; 我已经在struct webit之前推出了这个函数 tramafunction(); 这是函数的工作方式:接收一个用逗号分隔的字符串,然后将数据拆分为15个不同的变量,这是一个字符串的示例: /*¶bL0 L3,01,+08590323,-079343001,010215,00000000000000,-tN,000,012689997,001219456,000,7FF2,C07F,0,4,*/ function: trama function(){ struct webit wbt; char buf[103]=””; scanf(“%[^\t\n]s”,buf); printf(“\n \n Trama Recibida=[%s]\n\n”, […]

MPI发送和接收不能超过8182倍

我遇到以下代码的麻烦: int main(int argc, char *argv[]){ int id, p, n, ln, i, j, retCode; double *buffer; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &p); MPI_Comm_rank(MPI_COMM_WORLD, &id); n = strtol(argv[1],NULL,10); // total number of elements to be distributed ln = n/p; // local number of elements buffer = (double*)calloc(ln, sizeof(double)); if (id == p-1) // Process p-1 send to other […]

如何在fgets中使用feof和ferror(C中的minishell)

我写过这个minishell,但我不确定我是否正确控制错误。 我知道fgets可以返回feof和ferror( http://www.manpagez.com/man/3/fgets/ ),但我不知道如何使用它们。 我已经检查过fgets是否返回一个空指针(表示缓冲区的内容是inditerminate),但我想知道如何使用feof和ferror。 #include #include #include #include #define LINE_LEN 50 #define MAX_PARTS 50 int main () { char* token; char str[LINE_LEN]; char* arr[MAX_PARTS]; int i,j; bool go_on = true; while (go_on == true){ printf(“Write a line:(‘quit’ to end) \n $:”); fgets(str, LINE_LEN, stdin); if (str==NULL) { goto errorfgets; } else { size_t l=strlen(str); […]

使用CSparse库在C中表示稀疏矩阵

我无法理解如何使用CSparese库轻松表示C中的稀疏矩阵。 这就是我想要的 | 6.0 0.0 2.0 | A = | 3.0 8.0 0.0 | | 6.0 0.0 1.0 | with | 40.0 | b = | 50.0 | | 30.0 | csparse的cs Struct就是这个 typedef struct cs_sparse /* matrix in compressed-column or triplet form */ { csi nzmax ; /* maximum number of entries */ csi […]

打开外部程序

我一直试图在C中打开像编辑器这样的外部程序。我已经搜索了几个小时但没有找到打开外部可执行文件的方法,例如从控制台应用程序打开Skype等。 到目前为止这是我的代码: /* fopen1.c */ #include #include #include #include int main(int) { FILE *fp; fp = fopen(“C://Users/Jonte/Desktop/Skype.exe”, “r”); } 如何打开外部文件? 谢谢,真诚的,贝林

使用calloc和数组并返回指针的麻烦

作为参考,这是我的任务的第二部分: int* generateFibonacci(int size); 此函数将输入一个称为size的整数作为输入。 size变量中包含的值将表示要放入数组的Fibonacci序列中的数量。 该函数将使用calloc创建此大小的数组,然后使用Fibonacci序列中的size编号填充数组,从1和1 。 当数组完成时,函数将返回指向它的指针。 当我在第8行中收到错误warning: assignment makes and integer from pointer without a cast时,我遇到了麻烦:在warning: assignment makes and integer from pointer without a cast 。 我得到的另一个错误是在第19行“ warning: return makes pointer from integer without a cast ”。 所以我的问题是,我怎么想设置calloc来使用一个大小来自用户的数组,然后返回一个指向它的指针? #include #include int* generateFibonacci(int size) { int i, array[size]; array[size]=(int*)calloc(size, sizeof(int)); array[0]=0; array[1]=1; for(i […]

Linux C选择:管道回显到输入工作,但从键盘读取不?

我想了解http://beej.us/guide/bgnet/examples/select.c (包含在下面以供参考)。 我这样做: :~$ cat /etc/issue Ubuntu 10.04 LTS \n \l :~$ gcc –version gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 :~$ wget http://beej.us/guide/bgnet/examples/select.c :~$ gcc select.c -o select :~$ echo “ff” | ./select A key was pressed! :~$ ./select TYPINGTYTimed out. :~$ cat /etc/issue Ubuntu 10.04 LTS \n \l :~$ gcc –version gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 :~$ […]

如何调用使用JNI从C返回String的Java方法?

有没有办法调用java方法,它在C中返回一个String ? 对于Integer它的工作原理如下: JNIEXPORT jint JNICALL Java_Client_getAgeC(JNIEnv *env, jobject callingObject, jobject employeeObject) { jclass employeeClass = (*env)->GetObjectClass(env, employeeObject); jmethodID midGetAge = (*env)->GetMethodID(env, employeeClass, “getAge”, “()I”); int age = (*env)->CallIntMethod(env, employeeObject, midGetAge); return age; } 我已经搜索了很长时间但是没有什么能用于String 。 最后,我想得到一个char* 提前致谢!

_Complex long int

TL;博士; 问: _Complex long int中int含义是什么? 为什么合法? 更长的版本 我将一些32位代码移植到64位安全。 在一个地方,我注意到它使用: static __complex__ long int i32X[256]; 转储搜索并替换为int32_t更改“long int”给了我一个编译错误。 complex是一个较旧的GNUism,由标准_Complex取代。这是一个重现问题的简短代码片段: #include #include typedef _Complex long int WhyAmILegal; typedef _Complex int32_t Snafu; typedef int32_t _Complex Snafu2; 在gcc下编译给出: complextest.c:6:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘Snafu’ typedef _Complex int32_t Snafu; ^~~~~ complextest.c:8:17: error: two or more data […]

如何从C中的文本文件中读取数字块

我有一个文件numbers.dat包含列格式的大约300个数字(浮点,负数)。 目标是首先使用300个数字填写numbers.dat,然后每次将100个数字提取到另一个文件中,例如n1.dat。 第二个文件n2.dat将包含来自numbers.dat的下一个100个数字,依此类推3个从number.dat获得的文件子集。 我无法理解如何考虑最后读取的第100个数字的位置,以便在preios提取的数字之后读取和获取下一个块的文件。 尝试Gunner提供的解决方案: FILE *fp = fopen(“numbers.dat”,”r”); FILE *outFile1,*outFile2,*outFile3; int index=100; char anum[100]; while( fscanf(fp,”%s”,anum) == 1 ) { if(index==100) { // select proper output file based on index. fprintf(outFile1,”%s”,anum); index++; } if(index >101) { fprintf(outFile2,”%s”,anum); index++; } } 问题是只写入一个数据。 什么应该是正确的过程?