Run time error when reading char from txt file

This is a discussion on Run time error when reading char from txt file within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am a newbie in C and was testing a code to read text file into struct array, ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    Run time error when reading char from txt file

    Hello everyone,
    I am a newbie in C and was testing a code to read text file into struct array, posted here (Best method to alloc memory for string and desalloc..)

    The code compiles but I get a run time error. Please someone help me out.

    Code:
    //.H file
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fcntl.h>
    
    #ifndef temp_H
    #define temp_H
    
    typedef struct temp {
    	char item[20];
    	char description[50];
    	char detail[10];
    	char manufacturer[12];
    	int cost;
    } *ptr;	// wow, structs are 
    
    void fillstruct(struct temp*,char*);
    void showstruct(struct temp*);
    int linein(int,char*);
    
    #endif
    
    //Definition file
    #include "template.h"
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fcntl.h>
    
    
    
    void fillstruct (struct temp *ptr,char *line){// pass in a pointer
    	char *tok=strtok(line," ");
    	strcpy(ptr->item,tok);       
    	tok=strtok(NULL,"\"");
    	strcpy(ptr->description,tok);       
    	tok=strtok(NULL," ");
    	strcpy(ptr->detail,tok);       
    	tok=strtok(NULL," ");
    	strcpy(ptr->manufacturer,tok);       
    	tok=strtok(NULL," ");
    	ptr->cost=atoi(tok);
    }
    
    void showstruct (struct temp *ptr) {
    	printf("\nDescription of \"%s\": %s\n"
    		"Made by: %s\n"
    		"Price: $%d\n"
    		"note -- %s\n",
    		ptr->item, ptr->description, ptr->manufacturer, ptr->cost, ptr->detail);
    }
    
    
    int linein (int fd, char *string) {
    	int i=0;
    	char ch;
    	while (ch!='\n') {
    		if (read(fd,&ch,1)!=1) break;
    		string[i]=ch;
    		i++;
    	}
    	return i;
    }
    
    //MAIN function
    #include "template.h"
    #include <stdio.h>
    #include <fcntl.h>
    
    
    int main (int argc, char *argv[]) {
    	int len,i=0,x, fd=open("example.txt",O_RDONLY);
    	char string[128];	// should fit
    	struct temp *example=(struct temp*)malloc(sizeof(*example));  
    	 
    	while ((len=linein(fd,string))>0) {
    		string[len]='\0';
    		if (i>0) example=realloc(example,(i+1)*sizeof(*example));
    		fillstruct(&example[i],string);
    		i++;
    	}
    	close(fd);
    	/* now let's see if this worked */
    	for (x=0;x<i;x++) showstruct(&example[x]);
    	free(example);
    	return 0;  
    }
    
    //text file
    top "The top part" red Bob 13   
    side "A side part" green Cleo 2000
    corner "Connects 3rd dimension" round MK 27

    Note: The line in red is the source of error: My understanding from debugging report is when i = 0 ch = -52 which ch never encounters '\n'. Is that right? If so how can I fix this?

    My debug report:

    ch -52 char
    i 0 int
    temp.exe!linein(int fd, char * string) Line 35 C

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    When you take someone else example code.

    First make sure it works without modifying it.

    Then, make modification that make the code better; not worse the way you did.

    Do NOT make the variable and type names harder to follow.
    Do NOT cast the result of malloc functions. FAQ > Casting malloc - Cprogramming.com
    Do NOT post multiple files in a single code block.
    Do name each code block so we know the filename (when multiple files are used)

    Edit: Do state your OS name and Compiler name and version.

    The use of the non standard open is NOT recommended for newbies.
    The fopen is more likely to be recommended for newbies.
    Also, open is not considered to be as standard therefore OS differences are more likely to happen.

    Tim S
    Last edited by stahta01; 06-03-2012 at 04:56 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by stahta01 View Post
    When you take someone else example code.

    First make sure it works without modifying it.

    Then, make modification that make the code better; not worse the way you did.

    Do NOT make the variable and type names harder to follow.
    Do NOT cast the result of malloc functions. FAQ > Casting malloc - Cprogramming.com
    Do NOT post multiple files in a single code block.
    Do name each code block so we know the filename (when multiple files are used)

    Tim S
    I agree with that but I had to make modifications because the program was giving errors when I simply copy pasted it.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    Quote Originally Posted by robogeek View Post
    I agree with that but I had to make modifications because the program was giving errors when I simply copy pasted it.
    Your OS name and Compiler name/version is?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    DO NOT use a C++ Compiler to compile C code.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading from beginning of file a second time
    By afflictedd2 in forum C++ Programming
    Replies: 1
    Last Post: 01-14-2011, 08:12 AM
  2. reading a char at a time from text
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2008, 11:27 AM
  3. Error if reading char or float
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-07-2007, 03:06 PM
  4. reading from other files one char at a time
    By jmoney in forum C Programming
    Replies: 10
    Last Post: 02-24-2003, 05:18 PM
  5. reading 1 char at a time
    By krappykoder in forum C++ Programming
    Replies: 16
    Last Post: 10-23-2002, 11:12 AM

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