Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main()
- {
- FILE *txtFile = NULL, *binFile = NULL;
- char *string[15];
- int *firstIntArr, *secondIntArr, ch = 0, i, row = 0, srow = 0, cow = 0;
- double *doubleArr;
- for (i = 0; i < 15; i++){
- string[i] = (char *)malloc(15);
- }
- firstIntArr = (int *)malloc(15 * sizeof(int));
- secondIntArr = (int *)malloc(15 * sizeof(int));
- doubleArr = (double *)malloc(15 * sizeof(double));
- txtFile = fopen("txtFile.txt", "r");
- binFile = fopen("binFile", "wb");
- if (binFile == NULL || txtFile == NULL) {
- printf("File cannot be open!\n");
- exit(1);
- }
- printf("from txt file:\n");
- while (ch != EOF)
- {
- while ((ch = fgetc(txtFile)) != ';' && ch != EOF) {
- printf("%c", ch);
- string[srow][cow] = (char)ch;
- cow++;
- }
- printf("\n");
- while ((ch = fgetc(txtFile)) != ';' && ch != EOF) {
- printf("%c", ch);
- firstIntArr[row] = (int)ch;
- row++;
- }
- printf("\n");
- row = 0;
- while ((ch = fgetc(txtFile)) != ';' && ch != EOF) {
- printf("%c", ch);
- secondIntArr[row] = (int)ch;
- row++;
- }
- printf("\n");
- row = 0;
- while ((ch = fgetc(txtFile)) != ';' && ch != EOF) {
- printf("%c", ch);
- doubleArr[row] = (double)ch;
- row++;
- }
- srow++;
- cow = 0;
- printf("\n");
- }
- fclose(txtFile);
- for (i = 0; i<15; i++){
- if (fwrite(&string[i], 1, sizeof(string), binFile) != sizeof(string)) {
- printf("Err!");
- }
- if (fwrite(&firstIntArr[i], sizeof(int), 1, binFile) != 1) {
- printf("Err!");
- }
- if (fwrite(&secondIntArr[i], sizeof(int), 1, binFile) != 1) {
- printf("Err!");
- }
- if (fwrite(&doubleArr[i], sizeof(double), 1, binFile) != 1) {
- printf("Err!");
- }
- }
- fclose(binFile);
- for (i = 0; i < 15; i++)
- {
- free(string[i]);
- }
- free(firstIntArr);
- free(secondIntArr);
- free(doubleArr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment