int nb_cols_lfmm(char *file) { FILE *fp = fopen_read(file); int cols = 0; int c; char *szbuff; char *token; c = fgetc(fp); while ((c != EOF) && (c != 10) && (c!= 13)) { // count only columns (no space or tab) c = fgetc(fp); cols++; } fclose(fp); // open file fp = fopen_read(file); szbuff = (char *)calloc(2 * cols, sizeof(char)); // read first line token = fgets(szbuff, 2 * cols, fp); cols = 0; // count for first line token = strtok(szbuff, SEP); while (token) { cols++; token = strtok(NULL, SEP); } fclose(fp); free(szbuff); return cols; }
/* * readfromfile() * * The read callback that this function may use can return a value larger than * 'size' (which then this function returns) that indicates a problem and it * must be properly dealt with */ static size_t readfromfile(struct Form *form, char *buffer, size_t size) { size_t nread; bool callback = (form->data->type == FORM_CALLBACK)?TRUE:FALSE; if(callback) { if(form->fread_func == ZERO_NULL) return 0; else nread = form->fread_func(buffer, 1, size, form->data->line); } else { if(!form->fp) { /* this file hasn't yet been opened */ form->fp = fopen_read(form->data->line, "rb"); /* b is for binary */ if(!form->fp) return (size_t)-1; /* failure */ } nread = fread(buffer, 1, size, form->fp); } if(!nread) { /* this is the last chunk from the file, move on */ if(form->fp) { fclose(form->fp); form->fp = NULL; } form->data = form->data->next; } return nread; }
int nb_ind_ancestrymap(char *input_file) { FILE* File=NULL; int max_char_per_line = 1000; int nb = 0, diff = 0; char szbuff[max_char_per_line]; char *token; char tmp[512] = ""; // open input file File = fopen_read(input_file); // first line token = fgets(szbuff, max_char_per_line, File); token = strtok(szbuff, SEP); strcpy(tmp,token); // while still the same SNP, count while (!feof(File) && !diff) { token = fgets(szbuff, max_char_per_line, File); token = strtok(szbuff, SEP); // compare the current name with the first name diff = strcmp(tmp,token) != 0; nb ++; } // close file fclose(File); return nb; }
void pki_x509req::fload(const QString fname) { FILE *fp = fopen_read(fname); X509_REQ *_req; int ret = 0; if (fp != NULL) { _req = PEM_read_X509_REQ(fp, NULL, NULL, NULL); if (!_req) { pki_ign_openssl_error(); rewind(fp); _req = d2i_X509_REQ_fp(fp, NULL); } fclose(fp); if (ret || pki_ign_openssl_error()) { if (_req) X509_REQ_free(_req); throw errorEx(tr("Unable to load the certificate request in file %1. Tried PEM, DER and SPKAC format.").arg(fname)); } } else { fopen_error(fname); return; } if (_req) { X509_REQ_free(request); request = _req; } autoIntName(); if (getIntName().isEmpty()) setIntName(rmslashdot(fname)); openssl_error(fname); }
void read_ancestrymap (char* input_file, int N, int M, int* data) { FILE *File = NULL; int i, j, allele; char tmp[512] = ""; char ref[512] = ""; int max_char_per_line = 1000; char szbuff[max_char_per_line]; int warning = 0; // open input file File = fopen_read(input_file); j = 0; i = 0; // while not the end of the file // or not too many SNPs (j < M-1) // or last SNP but not too many ind (j == (M-1) && i< N)) while (fgets(szbuff,max_char_per_line, File) && (j < M-1 || (j == (M-1) && i< N))) { // read line read_line_ancestrymap(szbuff, &allele, tmp, i+1, j+1, input_file, &warning); // first SNP, save SNP name if (j == 0 && i == 0) strcpy(ref, tmp); // new SNP if (strcmp(ref, tmp) != 0) { // test if the number indiviual for the line is ok test_column (input_file, File, i, j+1, N, NULL); // new line i = 0; j++; // save new SNP name strcpy(ref, tmp); } // write genotype data[i * M + j] = allele; i++; } test_column (input_file, File, i, j+1, N, NULL); // test the number of lines test_line(input_file, File, j+1, M); fclose(File); }
void pki_multi::fload(const QString fname) { FILE * fp; BIO *bio = NULL; fp = fopen_read(fname); if (!fp) { fopen_error(fname); return; } bio = BIO_new_fp(fp, BIO_CLOSE); fromPEM_BIO(bio, fname); BIO_free(bio); };
int nb_cols_geno(char *file) { FILE *fp = fopen_read(file); int cols = 0; int c; // count the number of elements until EOF or "\n" c = fgetc(fp); while ((c != EOF) && (c != 10) && (c != 13)) { cols++; c = fgetc(fp); } fclose(fp); return cols; }
int nb_lines(char *file, int M) { FILE *fp = fopen_read(file); int lines = 0; int max_char_per_line = 20 * M + 10; char *szbuff = (char *)calloc(max_char_per_line, sizeof(char)); // while not end of file while (fgets(szbuff, max_char_per_line, fp)) // count lines++; fclose(fp); free(szbuff); return lines; }
int MainWindow::open_default_db() { if (!dbfile.isEmpty()) return 0; FILE *fp = fopen_read(getUserSettingsDir() + QDir::separator() + "defaultdb"); if (!fp) return 0; char buff[256]; size_t len = fread(buff, 1, 255, fp); fclose(fp); buff[len] = 0; dbfile = filename2QString(buff).trimmed(); if (QFile::exists(dbfile)) return init_database(); dbfile = QString(); return 0; }
void read_data_int(char *file_data, int N, int M, int *dat) { FILE *m_File = NULL; int i = 0; int j = 0; int max_char_per_line = MAX_LENGTH_NB_INT * M + 20; char *szbuff; char *token; // allocate memory szbuff = (char *) malloc(max_char_per_line * sizeof(char)); // open file m_File = fopen_read(file_data); i = 0; while (fgets(szbuff, max_char_per_line, m_File) && (i < N)) { j = 0; // cut line with split character SEP (" ") token = strtok(szbuff, SEP); // read elements and register them in dat while (token && j < M) { //printf("%s\n",token); dat[i * M + j] = (int)atof(token); // next elements token = strtok(NULL, SEP); j++; } i++; // check the number of columns test_column(file_data, m_File, j, i, M, token); } // check the number of lines test_line(file_data, m_File, i, N); // close file fclose(m_File); // free memory free(szbuff); }