Stuck on Grabbing a title from a text file

tdark

I am attempting to grab a title from a text file in a way that is completely new to me. My code is set up as follows:

struct sequence   
 { char *name; 
   char *sequence;  
   int  sequencelen;
 };

int main(int argc, char *argv[]) 
{
    struct sequence *newseq;
    getsequence("test.txt", newseq);

}


 void getsequence(const char *file, struct sequence *seq)
{
    FILE *fp = fopen(file, "r");
    struct sequence *seqptr = malloc(sizeof(*seq));
    char c;

    if (!fp)
    {
        exit(1);
    }

    while ((c = fgetc(fp)) != "\n")
    {
        if (c == '>')
            continue;

        strcat(seqptr -> name, c);
    }

    printf("Name: %s", seqptr -> name); //Expected output: this_is_a_new_sequence_title
}

The structure for the text file is as follows:

>this_is_a_new_sequence_title

Using structs in this way is, like I said, new to me, but seeing how it is another way to use them I would like to know how to do it. I am unsure, however, if I'm using them correctly, especially with regards to the strcat function.

Do I have to dynamically allocate memory for the struct's member variables, and, if so, how would I go about doing that? Or do I just have everything horribly wrong?

unwind

You're never allocating memory for the string. So when you call strcat(), the destination string is uninitialized memory, leading to undefined behavior.

Also, the 2nd argument to strcat() is a string, not a character. That's more undefined behavior as the library function interprets the single character as the address of a string.

You need to initialize storage space for sequence when you allocate it. Also, for code like this (dynamic strings) it's good to separate "allocated room" from "string length", and store both.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Trouble grabbing text from a website using Jsoup

分類Dev

Grabbing lines from text but fail to get multiple returns

分類Dev

Grabbing specific data from json

分類Dev

Grabbing data from certain files

分類Dev

Export from GAMs to a text file

分類Dev

Creating Navigation from text file

分類Dev

Extract usernames from text file

分類Dev

Adding users from text file

分類Dev

Reading from a text file not working

分類Dev

copy text from file to another

分類Dev

Grabbing a body of text using regex excluding specific conditions

分類Dev

Replace line in text file with line from other text file

分類Dev

Taking data from one text file and moving it to a new text file

分類Dev

php not grabbing zip code from freegeoip.net

分類Dev

Multiple consumer threads grabbing the same item from a queue

分類Dev

extract text from beautifulsoup lxml file

分類Dev

Create a matrix from a text file - python

分類Dev

Retrieve matching strings from text file

分類Dev

Using sed to extract strings from a text file

分類Dev

Printing the smallest people from a text file - Java

分類Dev

Load keras model from text file

分類Dev

Removing dynamic text from a temp file in a script

分類Dev

Extract substrings from a text file on each line?

分類Dev

Extract a property value from a text file

分類Dev

Reading from a text file and executing contents in terminal

分類Dev

Reading one column from a text file

分類Dev

Removing the second line from a text file

分類Dev

Reading from third line of text file

分類Dev

Python - Parsing multiple lines of text from file