Thread: File Handling

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    File Handling

    While reading strings from a file, I'm having a lil problem, what's wrong here it doesnt give any error or warning but while running it gets crashed.

    Code:
    int main(){
        FILE* r;
        int i=0; int k=0;
        int number[SIZE];
        int number2[SIZE];
        char *name[SIZE];
        char *name2[SIZE];
        
        r=fopen("xxx.dat","r");
        for ( i=0;i<SIZE;i++){
            name[i]=(char*)malloc(15*sizeof(char));
            name2[i]=(char*)malloc(15*sizeof(char ));
            }
    i=0;
    
        do{
              fscanf( r,"%d %s %s %d",&number[i],&name[i],&name2[i],&number2[i]);
              i++;}while(i!=SIZE); //I put the string's anpersants away but still gets crashed.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by m0ntana View Post
    While reading strings from a file, I'm having a lil problem, what's wrong here it doesnt give any error or warning but while running it gets crashed.

    Code:
    int main(){
        FILE* r;
        int i=0; int k=0;
        int number[SIZE];
        int number2[SIZE];
        char *name[SIZE];
        char *name2[SIZE];
        
        r=fopen("xxx.dat","r");
        for ( i=0;i<SIZE;i++){
            name[i]=(char*)malloc(15*sizeof(char));
            name2[i]=(char*)malloc(15*sizeof(char ));
            }
    i=0;
    
        do{
              fscanf( r,"&#37;d %s %s %d",&number[i],&name[i],&name2[i],&number2[i]);
              i++;}while(i!=SIZE); //I put the string's anpersants away but still gets crashed.
    Anyway I've solved the problem by doing this

    Code:
    do{
              fscanf( r,"%d %s %s %d",&number[i],&temp,&temp2,&number2[i]);
              strcpy(name[i],temp);
              strcpy(name2[i],temp2);
              i++;}while(i!=SIZE);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Stop using & where you don't need them.
    Whilst they're harmless if you have an array, when you have a pointer, it's a disaster.

    Assuming you're not overflowing the strings, then this should work.
    fscanf( r,"&#37;d %s %s %d",&number[i],name[i],name2[i],&number2[i]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by Salem View Post
    Stop using & where you don't need them.
    Whilst they're harmless if you have an array, when you have a pointer, it's a disaster.

    Assuming you're not overflowing the strings, then this should work.
    fscanf( r,"%d %s %s %d",&number[i],name[i],name2[i],&number2[i]);
    Hi Salem,

    Usually I dont use & while reading strings but when it doesn't work and get crashed, I tried to do by using it..anyway Even if I'm not overflowing the strings (maybe less not over) , it still blows off... That's why I used a temp string, and solved my problem by reading into temp and copying them into actual strings that I wanted to use...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM