Thread: Syntax help.

  1. #1
    Registered User
    Join Date
    Sep 2008

    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]

    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

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, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM