how to format data

Pages: 12
@mary00,
I have set the input and output streams (in and out) so that it is immediate to run the program in cpp.sh (the online compiler). However, you can comment these out and uncomment the alternative lines that will allow you to use input and output with files.
Thank you very much all...They asked me to convert this into a C program. What can you suggest me? I'm desperate
mary00 wrote:
They asked me to convert this into a C program. What can you suggest me?

I suggest that you tell them that it's far easier to do in c++.
If the above doesn't seem reasonable then you need to start over. Trying to "convert" a C++ program to a C program is usually not worth the effort so starting over would be the best way to go.

If you have problems with this C program, post your code showing your best effort and then ask specific questions based on the code posted.

so i have this input file:
Sesión 1 (COPY MODE):
Número de secuencias: 15

CORRIDA 1:
CARACTERES A DELETREAR : CALOR

CORRIDA 2:
CARACTERES A DELETREAR : CARINO

CORRIDA 3:
CARACTERES A DELETREAR : SUSHI

I'm trying with this code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main() {
FILE* fd, * file_out;
char buf[200];
char* res;
int secuencias, Sesión;

fd = fopen("summary.txt", "r");
if (fd == NULL) {
perror("Error");
exit(1);
}

file_out = fopen("ordinated.txt", "w");
if (file_out == NULL) {
printf("Error");
return -1;
}

// The %d indicates where the integer we are reading is
fscanf(fd, "Sesión %d (COPY MODE):\n\n", &Sesión);
fscanf(fd, "number of sequences: %d\n\n", &secuencias);

printf("Sesión: %d\Secuencias: %d\n", Sesión, secuencias);

while (!feof(fd)) {
res = fgets(buf, 200, fd);
if (res == NULL)
break;

printf("%s", buf);
}
}


But it doesn't tabulate a final file with only numbers and caracteres a deletrear...
But it doesn't tabulate a final file with only numbers and caracteres a deletrear...


What do you mean by "tabulate"? It would probably be better if you would show what you want you output to be with the given input file.

Also please use code tags when posting code.

I don't recommend that you have "text" within your fcanf() format strings, just "retrieve" and discard the unwanted "text" from the file using a suitable string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
int main() {
    FILE *fd, *file_out;
    char buf[200], rec[200];
    int sequences, session, i;
 
    fd = fopen("summary.txt", "r");
    if (fd == NULL) {
        perror("Error");
        exit(1);
    }
 
    file_out = fopen("ordinated.txt", "w");
    if (file_out == NULL) {
        printf("Error");
        return -1;
    }
 
    while ( !feof(fd) ) {
        fscanf(fd, "session %d (COPY MODE):\n\n", &session);
        fscanf(fd, "number of sequences: %d\n\n", &sequences);
 
        while (1) {
            strcpy(rec, buf); // Save previous copy of buff so we can check if
 
            fscanf(fd, "registration %d:\n\ncharacters recognized : %s\n\n", &i, buf);
       
            if (!strcmp(rec, buf)) // Indicates end of session data
               break;
 
            fprintf(file_out, "%d %d %d %s\n", session, i, sequences, buf);
        }
    }
 
    // Make sure to close the files
    fclose(fd);
    fclose(file_out);
}


The file input is that one...https://pastebin.com/fXHCmrm5
I don't know how to read the entire file and finally have an output file like this one https://pastebin.com/XVczR1E9

Hope someone could help me, I think that I sould allocate a malloc but I'm not succeeding
Last edited on
I know that I have a problem with char buf[200], rec[200]; and visual studio says that I shoul allocate heap memory but I don't know how...
Also I don't know how to make an effective loop of scanf/printf so that the program records the lines already written in a strcopy and then continues to read and write until the end of the input file
You are not properly reading the file. The file looks like:

1
2
3
4
5
6
7
8
9
10
11
12
session|registration|characters given|characters recognized
 
1 1 15 CALOR
1 2 15 CARINO
1 3 15 SUSHI
2 1 15 SUSHI SUSHI
3 1 15 PERA PFRA
3 2 15 SALON SALON
3 3 15 PERRO PERRO
4 1 7 TORTUGA TORTUGA
4 2 4 ANAEROBIO ANAERPBIO
4 3 4 PAPELES PAPELEX


So the first thing you must do is read and discard that first line.

Next you have a blank line that you must also read and discard.

Now we come to the actual data.

Please explain what you think is going to happen with this line: fscanf(fd, "session %d (COPY MODE):\n\n", &session);. Hint it is going to barf on your data lines since they don't contain "session", or "(COPY MODE):\n\n".

Your fscanf() format specifier should look something like: "%d%d%d%199%[^\n]" since you have data lines that appear to contain three integer values and one (maybe two) string variables.

You should also be using the return value from fscanf() to control your read loop, using EOF will usually cause problems.

Sorry, I didn't specifiy that the first line and the next blank line aren't important, I added them just to specifiy better how the output file should look like.
This line fscanf(fd, "session %d (COPY MODE):\n\n", &session);. actually "scans" the first line of the input line; sorry, I didn't paste the correct link of the input file https://pastebin.com/fXHCmrm5

The output file is right

What do you mean with "You should also be using the return value from fscanf() to control your read loop, using EOF will usually cause problems."?
Last edited on
can someone help me please??? I don't know how to solve with foef problem...
Topic archived. No new replies allowed.
Pages: 12