Thread: Trouble with Output .txt

  1. #1
    Registered User Leandro's Avatar
    Join Date
    Jan 2013
    Posts
    6

    Trouble with Output .txt

    Hello everyone!

    This is my first post, so take it easy... hehe
    I'm a begginer in programming, started few weeks ago, and I choose C as the first language to learn since I faced this task at while studying. So, let me explain what's going on... Well, I have a .txt file that contains, together with a few characters, columns of values that I want to save in different files like is written in the program (file 1, file2, file3 - a with x, b with y, c with z).

    The pattern of the source text file is like this:

    known_vector frame1 151 65 0 frame2 151.000763 64.538582 0.563737
    known_vector frame1 152 65 0 frame2 152.000702 64.542488 0.560822
    known_vector frame1 153 65 0 frame2 153.000671 64.546150 0.558089
    .
    .
    .

    Until now I could manage to split the files, but the output gives me only zeros.
    First the program count the number of lines of the read text file, then it should display the desired columns of double values in three other .txt files.

    I've got for the three .txt files columns like this:

    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
    .
    .
    .

    Could someone help me??? Probably it is an easy to solve mistake, once everything is new for me...

    Extr.c

    Cheers!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First, it's generally better to inline your code in code tags, like so.
    Code:
    #include <malloc.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int i, count;
    double *a, *b, *c;
    double *x, *y, *z;
    char tag[5][255];
    
    int main(int argc, char *argv[])
    {
    
    char line[255];
    
    FILE *fp, *file1, *file2, *file3;
     
    long NumberOfLines = 0;
     
     
    	if (argc != 5)
     
    	{
    		printf ("Usage: %s filename newfilename1 newfilename2 newfilename3 \n", argv[0]);
     
    		exit(1);
     	}
     
    	if ((fp = fopen(argv[1], "r")) == NULL)
     
    	{
    		printf("Can't open %s\n", argv[1]);
     
    		exit(1);
    	}
     
    	while( fgets(line,sizeof(line),fp) != NULL)   NumberOfLines++;
    
    		
    fgets(tag[0], 255, fp);
    fgets(tag[1], 255, fp); 
    
    count = NumberOfLines;
    
    a=calloc(count, sizeof(double));
    b=calloc(count, sizeof(double));
    c=calloc(count, sizeof(double));
    x=calloc(count, sizeof(double));
    y=calloc(count, sizeof(double));
    z=calloc(count, sizeof(double));
    
    
    	if ( (file1 = fopen(argv[2], "w" )) == NULL )
    	{ 
    		printf ("\n Cannot read the new file \n"); 
    	} 
    
    	if ( (file2 = fopen(argv[3], "w" )) == NULL )
    	{ 
    		printf ("\n Cannot read the new file \n"); 
    	} 
    
    	if ( (file3 = fopen(argv[4], "w" )) == NULL )
    	{ 
    		printf ("\n Cannot read the new file \n"); 
    	} 
    
    
    
    	for ( i=0; i<count; i++ )
    	{ 
    		fscanf (fp,"%s %s %lf %lf %lf %s %lf %lf %lf", tag[0], tag[1], &a[i], &b[i], &c[i], tag[2], &x[i], &y[i], &z[i]);
    		
    		// The pattern in the read file was like this: 
    		// "known_vector_chest_surface  frame1   151    65    0    frame2  151.000763 64.538582 0.563737"
    		//          tag[0]	        tag[1]  &a[i] &b[i] &c[i]  tag[2]    &x[i]      &y[i]     &z[i]
    		
    		fprintf (file1, "%lf  \t\t %lf \n", a[i], x[i]);
    		fprintf (file2, "%lf  \t\t %lf \n", b[i], y[i]);
    		fprintf (file3, "%lf  \t\t %lf \n", c[i], z[i]);
    	}
    
    free(a);
    free(b);
    free(c);
    free(x);
    free(y);
    free(z);
    
    return 0;
    }
    > while( fgets(line,sizeof(line),fp) != NULL) NumberOfLines++;
    When you've reached the end of the file, then all following fgets/fscanf calls will fail - unless you do something.

    The something being
    rewind(fp);
    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.

  3. #3
    Registered User Leandro's Avatar
    Join Date
    Jan 2013
    Posts
    6
    Thanks, Salem! It worked just fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-19-2012, 02:29 PM
  2. Replies: 5
    Last Post: 04-25-2011, 01:12 PM
  3. trouble writing stored data to output
    By gkoenig in forum C Programming
    Replies: 5
    Last Post: 03-31-2008, 12:23 PM
  4. Trouble displaying output
    By Guti14 in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2003, 08:39 PM
  5. Trouble with string output...Help!
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 06-08-2002, 02:26 PM