Syntax help.

This is a discussion on Syntax help. within the C Programming forums, part of the General Programming Boards category; Hey everyone, I am trying to read every line in a text file and write it to a binary file. ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    Syntax help.

    Hey everyone,
    I am trying to read every line in a text file and write it to a binary file.
    Each line has a string without spaces followed by a tab and a non-negative number, such as:

    a 74
    abc 12
    thisisastring 4192

    There are some restrictions in writting the binary file but I d rather not go into any details.
    My question is how do I make the struct create a char * of length "string" and not of length "line". "line" being each individual line (ie: abc 12)
    When I try declaring "string" before the struct definition the compiler complaints.

    The structs are to be of different size inside the binary file, that s why I cant just set the string variable to something like char text[255]
    Thanks

    Code:
    while(fgets(line, MAX_LEN, finp) != NULL) {                    //read a line from the text file
    	struct node {
    		unsigned char stringLen;
    		char text[strlen(line)]; //how do i make this text[strlen(string)]
    		int number;
    	};
    	typedef struct node MyNode;
    	MyNode dummy;
    		
    	string = (char *)strtok(line,"\t");                       //holds just the string of chars
    	numberString = (char *)strtok(NULL, " \n\t");   //holds the number
    	dummy.stringLen = strlen(string);                     //length of the string of chars
    	num = atoi(numberString);                                //convert to int
    	dummy.number = num;                                     
    	strcpy(dummy.text, string);
    	fwrite((const void *) &dummy, sizeof(MyNode), 1, fop);   //write to binary file

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    to dynamically allocate memory - use malloc

    and to not forget that the buffer you need should be strlen(line)+1 to store the nul-terminator as well
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21