Scanning string into structure

Kevin

Inside the text file, the first number is number of albums, second is number of tracks associated with a single album, and the number infront of each track title is the character length of the title.

Right now I am having trouble scanning in the name of each individual title (without the number in front) into char **tracks; which is also part of an array of Structs

For example, info[0].tracks[0] should print out the string "Like an umbrella".

Example Text File:

1
17
16 Like an umbrella
...
15 Dynasty Warrior

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct album 
{
     int num_tracks;
     char **tracks;

};

int main(int argc, char *argv[]){
int numbALBUMS=0, numbCharInTrack=0;
int i=0,j=0;

    FILE *albums;
    albums = fopen (argv[1], "r");

    fscanf(albums, "%d", &numbALBUMS);
    struct album *info = (struct album*)malloc(numbALBUMS * sizeof(struct album));

    for(i=0;i<numbALBUMS;i++){
        fscanf(albums, "%d", &info[i].num_tracks);
        info[i].tracks = malloc(sizeof(char*) * info[i].num_tracks);

            for(j=0;j<info[i].num_tracks;j++){
                fscanf(albums, "%d", &numbCharInTrack);
                info[i].tracks[j] = malloc(sizeof(char) * numbCharInTrack);

                //NEED HELP HERE

            }
    }


fclose(albums);
return 0;

}
BLUEPIXY

try this

fscanf(albums, "%d", &numbCharInTrack);
info[i].tracks[j] = malloc(sizeof(char) * (numbCharInTrack+1));//+1 for NUL, sizeof(char) is always 1(by standard)
fscanf(albums, " %[^\n]", info[i].tracks[j]);//Space to skip the previous space

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Scanning string into structure

From Dev

C structure not scanning all the inputs

From Dev

scanning a string until ":"

From Dev

Scanning an Array for String Matches in Ruby

From Dev

Java: Scanning string, then search for integer

From Dev

Scanning a string from a file ignoring the last character

From Dev

CGPDFStringGetBytePtr returning incorrect string while scanning pdf

From Dev

Python EOL while scanning string literal

From Dev

EOL whilst scanning string literal - Python

From Dev

Python EOL while scanning string literal

From Dev

Scanning a string from a file ignoring the last character

From Dev

Python error: EOL while scanning string literal

From Dev

CGPDFStringGetBytePtr returning incorrect string while scanning pdf

From Dev

Continue scanning a string until it has found the first/last occurrence of a string

From Dev

How to speed up scanning a big string with a big regex?

From Dev

C language:scanning of input string is skipped inside for loop

From Dev

SyntaxError: EOL while scanning string literal while using backslash escape

From Dev

Python SyntaxError: EOF while scanning triple-quoted string literal

From Dev

How to send string to EditText in Fragment after scanning in MainActivity

From Dev

How to type a string to end a integer Scanning process in Java

From Dev

Python EOF while scanning triple-quoted string literal

From Dev

Python simple loop EOL while scanning string literal

From Dev

Ruby: Find first N regex matches in a string (and stop scanning)

From Dev

Ruby string structure

From Dev

pointer to structure not returning a string

From Dev

Initialization of a structure containing string

From Dev

Converting String to Structure

From Dev

pointer to structure not returning a string

From Dev

assign a string to a structure variable in c

Related Related

  1. 1

    Scanning string into structure

  2. 2

    C structure not scanning all the inputs

  3. 3

    scanning a string until ":"

  4. 4

    Scanning an Array for String Matches in Ruby

  5. 5

    Java: Scanning string, then search for integer

  6. 6

    Scanning a string from a file ignoring the last character

  7. 7

    CGPDFStringGetBytePtr returning incorrect string while scanning pdf

  8. 8

    Python EOL while scanning string literal

  9. 9

    EOL whilst scanning string literal - Python

  10. 10

    Python EOL while scanning string literal

  11. 11

    Scanning a string from a file ignoring the last character

  12. 12

    Python error: EOL while scanning string literal

  13. 13

    CGPDFStringGetBytePtr returning incorrect string while scanning pdf

  14. 14

    Continue scanning a string until it has found the first/last occurrence of a string

  15. 15

    How to speed up scanning a big string with a big regex?

  16. 16

    C language:scanning of input string is skipped inside for loop

  17. 17

    SyntaxError: EOL while scanning string literal while using backslash escape

  18. 18

    Python SyntaxError: EOF while scanning triple-quoted string literal

  19. 19

    How to send string to EditText in Fragment after scanning in MainActivity

  20. 20

    How to type a string to end a integer Scanning process in Java

  21. 21

    Python EOF while scanning triple-quoted string literal

  22. 22

    Python simple loop EOL while scanning string literal

  23. 23

    Ruby: Find first N regex matches in a string (and stop scanning)

  24. 24

    Ruby string structure

  25. 25

    pointer to structure not returning a string

  26. 26

    Initialization of a structure containing string

  27. 27

    Converting String to Structure

  28. 28

    pointer to structure not returning a string

  29. 29

    assign a string to a structure variable in c

HotTag

Archive