displaying text files, wierd thing :(

This is a discussion on displaying text files, wierd thing :( within the C Programming forums, part of the General Programming Boards category; Hi, this question is about displaying a text file on the scree, but don't get mad (please). I have made ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    207

    Exclamation displaying text files, wierd thing :(

    Hi, this question is about displaying a text file on the scree, but don't get mad (please).

    I have made several functions that display text files, but now I'm having a strange problem. My program handles different type of files, one of them is file_name.mat (materials files). This files are of the type:

    5.00 2500.00
    7.00 5000.00
    9.00 7500.00


    Being the left column diameters and the right column breaking loads.

    I have a function that creates materials, one that loads materials and one that display the properties of the materials.

    I wrote the code for the three of them, it compiled with no errors and it run OK. But when I've run the program again, I tried to create new materials files, and it doesn't work properly. The "only" material that is displayed is the one called acero.mat.
    I attach a zipped folder with the whole code, the executable program and three materials files (the one that works and two that don't work).
    In case you only need to see the three functions I've mentioned, I include them here:

    Code:
    void
    new_material (void)
    {
    
       char  file_name[8];
       double diameter, load;
       int total, i;
    
       FILE *output;
    
       printf("Enter name of the new materials library: ");
       scanf("%s", file_name);
       strcat( file_name, ".mat" );
       output = fopen(file_name, "w");
    
       printf("Please enter the number of diameters to be entered:  ");
    
       scanf("%d", &total);
    
       for(i = 0; i < total; i++)
       {
         printf("Please enter a diameter and its breaking load: ");
         scanf("%lf %lf", &diameter, &load);
         fprintf(output, "%.2f %.2f\n", diameter, load);
       }
    
       fclose(output);
    
       printf("\n\nPress any key to continue");
       getch();
       clrscr();
       materials();
    
    
    }
    Code:
    void
    load_material (void)
    {
    
    	// Load an existing material to use with the calculations
    
       struct ffblk ffblk;
       int done, length;
    
       clrscr();
       printf("Directory listing of existing materials \n");
       done = findfirst("*.mat",&ffblk,0);
       while (!done)
    	 {
    	 length = strlen(ffblk.ff_name);
    	 ffblk.ff_name[length - 4] = 0;
    	 printf("  %s\n", ffblk.ff_name);
    	 done = findnext(&ffblk);
    	 }
    
    
       printf("\nName of the material you want to load ");
       scanf("%s", material);
       strcat( material, ".mat" );
       caracteristics_mat();
    
    }

    Code:
    void
    caracteristics_mat (void)
    {
    
       double getval;  // this variable GETs the VALues from the existing project
       char  file_name[PATH_MAX];
       int i, times;
       FILE *input;
    
       times = count_lines();
    
       clrscr();
       strcpy (file_name, material);
    
       input = fopen(file_name, "r");
    
       if (( input = fopen(file_name, "r+")) == NULL ) // check it for errors
           {
           printf("There was an error reading from the file!! \n");
           printf("Press any key to continue ");
           getch();
    //       main();
           return;
    
           }
    
       printf("The caracteristics of the actual material are: \n");
       printf("Diameter	Breaking Load \n\n ");
       for ( i = 0 ; i < times ; i++)
       {
    	fscanf(input, "%lf\n", &getval);
    	printf("\n  %.2f", getval);
    	fscanf(input, "%lf\n", &getval);
    	printf("\t\t%.2f\n", getval);
    
       }
    
       printf("\n\n		Press any key to continue");
       getch();
       return;
    
    }
    Attached Files Attached Files

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I wrote the code for the three of them, it compiled with no
    > errors and it run OK. But when I've run the program again, I
    > tried to create new materials files, and it doesn't work properly.
    > The "only" material that is displayed is the one called
    > acero.mat.

    > char file_name[8];

    > strcat( file_name, ".mat" );

    There's your problem. I'm actually surprised that 'acero.mat' works also. It's longer than 8 characters. Keep in mind that you're prompting for a user name, and appending four characters to the end of it. This means that including the NULL, you can only enter 3 letter file names to have a file name that fits in the space you have allocated for it.

    I haven't read further in your code, but I'd suggest you fix that first. Make it a 128 characters or something "safe". You also may want to use scanf("%20s", file_name ) or something, so you're limiting how many characters they enter.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    quzah: thanks, I forgot to include: char file_name[PATH_MAX];

    as I did in the other functions. Being PATH_MAX = 12.

    I've changed to ("%8s", file_name ), so that I have file names of 8 characters plus 4 characters being ".mat".

    The program still doesn't work properly, though.

    I think now it's too late for me to find/solve errors, so I'd just carry on tomorrow morning.

    Thanks a lot for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text files on the internet
    By Mavix in forum C# Programming
    Replies: 8
    Last Post: 06-20-2007, 06:38 AM
  2. LaTeX Question: Displaying Text and Images
    By Reisswolf in forum Tech Board
    Replies: 0
    Last Post: 06-30-2006, 09:01 AM
  3. Creating text files
    By Crash1322 in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2003, 09:55 PM
  4. Replies: 1
    Last Post: 07-13-2002, 06:45 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 10:45 PM

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