Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

wrting multiple substring in multiple text file

Status
Not open for further replies.

dayana42200

Junior Member level 3
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:

CTGCTTGTGAATTTTCTGAGACGGATGTAACAAATACTGAACATCATCAACCCAGTAATAATGATTTGAACACCACTGAGAAGCGTGCAGCTGAGAGGCATCCAGAAAAGTATCAGGGTAGTTCTGTTTCAAACTTGCATGTGGAGCCATGTGGCACAAATACTCATGCCAGCTCATTACAGCATGAGAACAGCAGTTTATTACTCACTAAAGACAGAATGAATGTAGAAAAGGCTGAATTCTGTAATAAAAGCAAACAGCCTGGCTTAGCAAGGAGCCAACATAACAGATGGGCTGGAAGTAAGGAAACATGTAATGATAGGCGGACTCCCAGCACAGAAAAAAAGGTAGATCTGAATGCTGATCCCCTGTGTGAGAGAAAAGAATGGAATAAGCAGAAACTGCCATGCTCAGAGAATCCTAGAGATACTGAAGATGTTCCTTGGATAACACTAAATAGCAGCATTCAGAAAGTTAATGAGTGGTTTTCCAGAAGTGATGAACTGTTAGGTTCTGATGACTCACATGATGGGGAGTCTGAATCAAATGCCAAAGTAGCTGATGTATTGGACGTTCTAAATGAGGTAGATGAATATTCTGGTTCTTCAGAGAAAATAGACTTACTGGCCAGTGATCCTCATGAGGCTTTAATATGTAAAAGTGAAAGAGTTCACTCCAAATCAGTAGAGAGTAATATTGAAGACAAAATATTTGGGAAAACCTATCGGAAGAAGGCAAGCCTCCCCAACTTAAGCCATGTAACTGAAAATCTAATTATAGGAGCATTTGTTACTGAGCCACAGATAATACAAGAGCGTCCCCTCACAAATAAATTAAAGCGTAAAAGGAGACCTACATCAGGCCTTCATCCTGAGGATTTTATCAAGAAAGCAGATTTGGCAGTTCAAAAGACTCCTGAAATGATAAATCAGGGAACTAACCAAACGGAGCAGAATGGTCAAGTGATGAATATTACTAATAGTGGTCATGAGAATAAAACAAAAGGTGATTCTATTCAGAATGAGAAAAATCCTAACCCAATAGAATCACTCGAAAAAGAATCTGCTTTCAAAACGAAAGCTGAACCTATAAGCAGCAGTATAAGCAATATGGAACTCGAATTAAATATCCACAATTCAAAAGCACCTAAAAAGAATAGGCTGAGGAGGAAGTCTTCTACCAGGCATATTCATGCGCTTGAACTAGTAGTCAGTAGAAATCTAAGCCCACCTAATTGTACTGAATTGCAAATTGATAGTTGTTCTAGCAGTGAAGAGATAAAGAAAAAAAAGTACAACCAAATGCCAGTCAGGCACAGCAGAAACCTACAACTCATGGAAGGTAAAGAACCTGCAACTGGAGCCAAGAAGAGTAACAAGCCAAATGAACAGACAAGTAAAAGACATGACAGCGATACTTTCCCAGAGCTGAAGTTAACAAATGCACCTGGTTCTTTTACTAAGTGTTCAAATACCAGTGAACTTAAAGAATTTGTCAATCCTAGCCTTCCAAGAGAAGAAAAAGAAGAGAAACTAGAAACAGTTAAAGTGTCTAATAATGCTGAAGACCCCAAAGATCTCATGTTAAGTGGAGAAAGGGTTTTGCAAACTGAAAGATCTGTAGAGAGTAGCAGTATTTCATTGGTACCTGGTACTGATTATGGCACTCAGGAAAGTATCTCGTTACTGGAAGTTAGCACTCTAGGGAAGGCAAAAACAGAACCAAATAAATGTGTGAGTCAGTGTGCAGCATTTGAAAACCCCAAGGGACTAATTCATGGTTGTTCCAAAGATAATAGAAATGACACAGAAGGCTTTAAGTATCCATTGGGACATGAAGTTAACCACAGTCGGGAAACAAGCATAGAAATGGAAGAAAGTGAACTTGATGCTCAGTATTTGCAGAATACATTCAAGGTTTCAAAGCGCCAGTCATTTGCTCCGTTTTCAAATCCAGGAAATGCAGAAGAGGAATGTGCAACATTCTCTGCCCACTCTGGGTCCTTAAAGAAACAAAGTCCAAAAGTCACTTTTGAATGTGAACAAAAGGAAGAAAATCAAGGAAAGAATGAGTCTAATATCAAGCCTGTACAGACAGTTAATATCACTGCAGGCTTTCCTGTGGTTGGTCAGAAAGATAAGCCAGTTGATAATGCCAAATGTAGTATCAAAGGAGGCTCTAGGTTTTGTCTATCATCTCAGTTCAGAGGCAACGAAACTGGACTCATTACTCCAAATAAACATGGACTTTTACAAAACCCATATCGTATACCACCACTTTTTCCCATCAAGTCATTTGTTAAAACTAAATGTAAGAAAAATCTGCTAGAGGAAAACTTTGAGGAACATTCAATGTCACCTGAAAGAGAAATGGGAAATGAGAACATTCCAAGTACAGTGAGCACAATTAGCCGTAATAACATTAGAGAAAATGTTTTTAAAGAAGCCAGCTCAAGCAATATTAATGAAGTAGGTTCCAGTACTAATGAAGTGGGCTCCAGTATTAATGAAATAGGTTCCAGTGATGAAAACATTCAAGCAGAACTAGGTAGAAACAGAGGGCCAAAATTGAATGCTATGCTTAGATTAGGGGTTTTGCAACCTGAGGTCTATAAACAAAGTCTTCCTGGAAGTAATTGTAAGCATCCTGAAATAAAAAAGCAAGAATATGAAGAAGTAGTTCAGACTGTTAATACAGATTTCTCTCCATATCTGATTTCAGATAACTTAGAACAGCCTATGGGAAGTAGTCATGCATCTCAGGTTTGTTCTGAGACACCTGATGACCTGTTAGATGATGGTGAAATAAAGGAAGATACTAGTTTTGCTGAAAATGACATTAAGGAAAGTTCTGCTGTTTTTAGCAAAAGCGTCCAGAAAGGAGAGCTTAGCAGGAGTCCTAGCCCTTTCACCCATACACATTTGGCTCAGGGTTACCGAAGAGGGGCCAAGAAATTAGAGTCCTCAGAAGAGAACTTATCTAGTGAGGATGAAGAGCTTCCCTGCTTCCAACACTTGTTATTTGGTAAAGTAAACAATATACCTTCTCAGTCTACTAGGCATAGCACCGTTGCTACCGAGTGTCTGTCTAAGAACACAGAGGAGAATTTATTATCATTGAAGAATAGCTTAAATGACTGCAGTAACCAGGTAATATTGGCAAAGGCATCTCAGGAACATCACCTTAGTGAGGAAACAAAATGTTCTGCTAGCTTGTTTTCTTCACAGTGCAGTGAATTGGAAGACTTGACTGCAAATACAAACACCCAGGATCCTTTCTTGATTGGTTCTTCCAAACAAATGAGGCATCAGTCTGAAAGCCAGGGAGTTGGTCTGAGTGACAAGGAATTGGTTTCAGATGATGAAGAAAGAGGAACGGGCTTGGAAGAAAATAATCAAGAAGAGCAAAGCATGGATTCAAACTTAG

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)

>1
CTGCTTGTGAATTTTCTGAGACGGATGTAACAAATACTGAACATCATCAACCCAGTAATAATGATTTGAACACCACTGAGAAGCGTGCAGCTGAGAGGCATCCAGAAAAGTATC

>2
AGGGTAGTTCTGTTTCAAACTTGCATGTGGAGCCATGTGGCACAAATACTCATGCCAGCTCATTACAGCATGAGAACAGCAGTTTATTACTCACTAAAGACAGAATGAATGTAG

>3
AAAAGGCTGAATTCTGTAATAAAAGCAAACAGCCTGGCTTAGCAAGGAGCCAACATAACAGATGGGCTGGAAGTAAGGAAACATGTAATGATAGGCGGACTCCCAGCACAGAAA

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:

                  ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F GCTGCTTGTGAATTTTCTGAGACGGATGTAACAAATACTGAACATCATCAACCCAGTAATAATGATTTGAACACCACTGAGAAGCGTGCAGCTGAGAGGCATCCAGAAAAGTATC   ’ & ! Æ0 `9 R }      "   Ü"!a: S ~x* ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ * ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ÊOv°iOvý_ýú  ˜v  xî` _ýúˆð` ˆð` xî`   ý_ýúpî` àø` ÊOv1!ÉŒþÿÿÿ°iOvôiOvó`  l÷`  lõ`  ä x@UvÀQUv¨v ö` pø`
ßNv  l÷`  lõ`  ä ´v ˜v ÁßNv,@| ˜v  ? H        h((((                  H„„„„„„„„„„‚‚‚‚‚‚          H0´uŒw¢ÝNh v @÷` … „… „… „ ( (
… „´uŒwÜNh v ÷` ÜNhT v *÷` … „¸v ŸþÿÿŸþÿÿ$ £þÿÿXv ¸v Üÿÿÿþÿÿ… „ ¨v å … „ v ¸v Àv v $ ü v £þÿÿ£þÿÿ ( À v ¦þÿÿØÿÿÿ~þÿÿ Àv ¦v ‡þÿÿŽfŒw $ ªÜNh(  … „+  …+  `ø`   … „À v    v Pv  $ å åPv  Xv  @ c…  Ø v … $ `v `v þÿÿÿÂv Ô÷` %w¢Ý¹þÿÿÿ8÷` yIŒw ¤v ÷` v … `v   Xv %wX÷` îGŒw îGŒw  Ë÷` x÷` *rNv v   ÷` wNv  ø÷`  ¨÷` Nv Ô÷` ø÷` à÷` Nvd
Nv NvÁZýú Tø`  Xv pø` ÁZýú´÷` pø` ž{Tv NvNvàv `NvTø` Jvàv Xv P v P v 71u+`ø` rnNv v }nNvAUýú P v –£Žw'èwÿÿÿÿ$ Lø`  µÿ©\ø`  }Tv lOvV:ŒwúÒNh`M| | hM| ˆbPvV:Œw’ÒNh @|  (@|   }Tvˆø`  `ù`  !ÉŒþÿÿÿºkOv  }Tv ûÿÿ }TvbPv`VMvù` þúÿÿ |  ôø` | ¸JvÀJv | p@ ¨ù` GhwøM| Œw   Ü|    VÒNh Tù` Ÿw0P@ p@ ´ù` ü@ 0P@   ’Þ  ô@ @  @ ¨ù` Æ¡Žw @  @ @ ú` Vì‹w @ @  tì‹wRÑNh ! `„v Œw @ ¼ Ìù` °å‹w8ú` | ŽfŒwÈW| ´uŒwšÐNhà | ü` `„v 8@| p@ @   ‹¸Œw”ü` ŽÒNhºÙ¹¤ü`  ,/’w  ÿÿÿÿ* ¢ $| Èû`  Äû` €<| Õ ç ÈW|   ûÿÿ   Ú| ´uŒw"×Nh@ | Àü` Tû` Hû` F¦Œw,Vˆw,Vˆwˆû`  `¦ŒwÐNh ç ã   ç ã  è  ,Vˆw $ý` L ^ °X| ¢ûÿÿ‡ ¢ûÿÿ¼û` ö¯Œw ÈW| ö¯Œw ¼û`  B| | îÐNhÈW|   À | ÈW| xû` ÈW| H  DX|  ȱŒwí±ŒwZ×Nhxü`  ü` xü`  ÈW| hM| Ðû` ü |»ŽwM·  | ¨X| ü` ÈW|  0ü` ¨X|  , °X| tü` È    ”ˆ v ŽfŒwðv ´uŒwJÕNh v hþ` | pþ` ¸ü` ´uŒw… „ … „ˆþ` … „Z v ˜þ`   … „@ … „ , … „@  îGŒw… „–£Žw$ $ÿÿÿÿŽþÿÿŽþÿÿ þÿÿðv ÿ` íÿÿÿ}þÿÿþÿÿþÿÿ¤v ’þÿÿàv V:ŒwÕNhØv v àv ¤v v   |   … „ À | v     À v   ïÿÿÿ}þÿÿ   … v  h  … „À v …L Øv   v    œv  èv    … àv  …      ´þ` w ݹ  P v T v 71u+xþ` rnNv v  P v X v 71u+˜þ` rnNv v }nNv¹Sýú P v –£Žw'èwÿÿÿÿ$ „þ`  µÿ©”þ` €@ P@ lOv\ v ¬þ` mPvCUvìþ` ˆbPv ÃkOvºkOvÍSýú€@ €@ P@ Àþ` øþ` Ìÿ` ÊOv!ÉŒþÿÿÿºkOv*lOvP@ ”ÿ` ®@ P@                   ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F GAGGGTAGTTCTGTTTCAAACTTGCATGTGGAGCCATGTGGCACAAATACTCATGCCAGCTCATTACAGCATGAGAACAGCAGTTTATTACTCACTAAAGACAGAATGAATGTAG

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

||=== Build: Debug in WordProcessing (compiler: GNU GCC Compiler) ===|
C:\Users\User\Desktop\WordProcessing\WordProcessing\WordProcessing.c||In function 'main':|
C:\Users\User\Desktop\WordProcessing\WordProcessing\WordProcessing.c|21|warning: comparison between pointer and integer|
C:\Users\User\Desktop\WordProcessing\WordProcessing\WordProcessing.c|27|warning: passing argument 1 of 'fputs' from incompatible pointer type [-Wincompatible-pointer-types]|
C:\Program Files (x86)\CodeBlocks\MinGW\include\stdio.h|356|note: expected 'const char *' but argument is of type 'char (*)[115]'|
C:\Users\User\Desktop\WordProcessing\WordProcessing\WordProcessing.c|6|warning: unused variable 't' [-Wunused-variable]|
||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top