wrting multiple substring in multiple text file

Status
Not open for further replies.

dayana42200

Junior Member level 3
Joined
Feb 9, 2018
Messages
31
Helped
0
Reputation
0
Reaction score
1
Trophy points
6
Activity points
315
Hello.

Im writing a C programme that reads the files and trying the save the processed string to other file.

So the string is:


The inital coding is:

Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        /*declare and initialise variable*/
        char message[32][115],buffer[115];
        int i=1;
        FILE *file_in;

        file_in=fopen("exon11.txt", "rb");

        /*stores and prints the data from the string*/
        while(fgets(buffer,115,file_in))
            {
                strcpy(message[i],buffer);
                printf(">%d\n%s\n",i,message[i]);
                i++;
        }
        getchar();
        return 0;

the Output of the above coding is (i have problem uploading the output image so ill just give the substring)


From the image, the code can generate 32 substring where each substring is in 115 character length from the input file. I didnt show the full generated output.

So Im tring to write the output image in a different text file.

my revised code:

Code:
        #include<stdio.h>
    #include<string.h>
    int main()
    {
        /*declare and initialise variable*/
        char message[32][115],buffer[115];
        int i=1;
        FILE *file_in;
        FILE *file_out;

        file_in=fopen("exon11.txt", "rb");
        file_out=fopen("1.txt", "wb");

        /*stores and prints the data from the string*/
        while(fgets(buffer,115,file_in))
            {
                strcpy(message[i],buffer);
                fwrite(message,sizeof(char),sizeof(message),file_out);
        }
        getchar();
        return 0;

and I get this:


do you guys know why?

also is it possible write those substring in different text file?

for example each of the 32 substring is written in 32 text file (subtring 1 in 1st text file, substring 2 in 2nd text file, etc ...)

Thank you in advance.
 
Last edited by a moderator:

You are opening and saving text files in binary mode, use 'rt' and 'wt' in the file commands.
I suspect you are also losing the final character of each string. Try increasing the buffer size to 116 and appending a NUL to the end of the file you have read before writing it out again. If the string isn't terminated by a NUL (0x00) the 'sizeof' might be seeing a longer string than you think. The data written out certainly looks like it continues beyond the end of the buffer and picks up some other data in memory until it comes across a 0x00 from somewhere else.

Brian.
 

@betwixt

Ive tried your suggestion as shown below

Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        /*declare and initialize variable*/
        char message[32][115],buffer[115],t;
        int i=1;
        FILE *file_in;
        FILE *file_out;

        file_in=fopen("exon11.txt", "r");


        /*stores and prints the data from the string*/
        if (file_in == NULL)
        {
            printf("No file or cannot read file\n");
        }
        else
        {
            while(fgets(buffer,115,file_in) != EOF)
            {
                strcpy(message[i],buffer);
                i++;

                file_out=fopen("2.txt", "w");
                fputs(message,file_out);
            /*printf(">%d\n%s\n",i,message[i]);
            */
             }
        getchar();
        return 0;
        }
    }

but Ive errors that say


from the error, I think I understand that puts can be used only for 1-dimension array.

Is there any suitable command that can be used to write from a 2-dimensional array from a text file?
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…