Thread: trouble writing stored data to output

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    77

    trouble writing stored data to output

    I am having trouble writing data to an output file. I am able to create the file but not able to put anything in it.

    Heres the relevant code:

    Code:
    else if ((strcmp ( command, "write")) == 0 )
                        {
                            
                            list_num = atoi(strtok(NULL, " \n\t")) ;
                            filename = strtok(NULL, " \n\t") ;
                            output = fopen(filename, "w") ;
                            
                            fputs(loadedSequences[list_num].header, output) ;
                            fputs(loadedSequences[list_num].data, output) ;
                            
                            fclose(output) ;
                        }
    The command will look like the following:
    SeqTool> write # <filename>

    Any help would be great.
    Last edited by gkoenig; 03-30-2008 at 02:54 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should check that fopen didn't fail (if it did, output == NULL). list_num looks like it should be the right thing. Do you have data in loadedSequences[list_num]? What are you getting?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    here is more code to show what i am doing

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    typedef struct { char *name ; char data[1000000] ; int length ; int type ; char header[1000] ; } sequence ;
    
    sequence loadedSequences[1000] ;
    
    int nSequences = 0 ;
        
    int main()
        {
            char std_input[1000] ;  // prev. buffer
            char header_data[1000] ;
            char seq_data[100000] ;
            char buffer[100000] ;
            char header_line[1000] ;
            
            FILE *input, *output ;
            
            char *seqName ;        
            char *command ;
            char *filename ;
            char *accession_id ;
        
            int list_num ;
            
            int i ;
            int c ;
            int n ;
            
            for(;;)
                {
                    printf("SeqTool> ") ;
                    
                    fgets(std_input, 1000, stdin) ;
                    fpurge(stdin) ;
                    
                    // Eliminating the newline character
                    char *p = strchr(std_input, '\n') ;
                    if (p) 
                        {
                            *p = '\0' ; 
                        } 
                                              
                    command = strtok( std_input, " \t\n" ) ; 
                    
    ////////////////// Load sequence in FASTA format //////////////////////////
                    
                    if( (strcmp ( command, "read" )) == 0 )
                        {
                            
                            filename = strtok(NULL, " \t\n" ) ; // collect filename                       
                            if( filename ) 
                                { 
                                    input = fopen(filename, "r") ;
                                    if ( !input )
                                        {
                                            perror(filename);
                                            continue ;
                                        }                                
                                }                            
                           
                            fgets( header_data, 1000, input) ; 
                                                    
                            seqName = &header_data[1] ;   // After the > in FASTA format
                            
                            strcpy( header_line, seqName ) ;  
                            
                            accession_id = strtok(header_line, " \n\t" ) ;
                           
                            loadedSequences[nSequences].name = 
                                (char *) malloc( ( strlen(accession_id) + 1 ) * sizeof( char ) ) ;
                                
                           
                           strcpy(loadedSequences[nSequences].header, seqName) ;
                           
                            
                           strcpy( loadedSequences[nSequences].name , accession_id ) ; 
                            
                            // Collect the sequence from FASTA file                                             
                            
                            n = 0 ;
                                                
                            while ((c = getc(input)) != EOF)
                                        {
                                            if(c >= 'A' && c <= 'Z')
                                                {
                                                    loadedSequences[nSequences].data[n++] = c ;
                                                 
                                                }
                                        }  
                                       
                            
                            loadedSequences[nSequences].length = strlen((loadedSequences[nSequences].data)) ;
                            
                            fclose(input) ;
                            
                            ++nSequences ;
                   
                        }
                        
    ///////////////// Write sequence in FASTA format //////////////////////////
                   
                    else if ((strcmp ( command, "write")) == 0 )
                        {
                            
                            list_num = atoi(strtok(NULL, " \n\t")) ;
                            filename = strtok(NULL, " \n\t") ;
                            output = fopen(filename, "w") ;
                            
                            fputs(loadedSequences[list_num].header, output) ;
                            fputs(loadedSequences[list_num].data, output) ;
                            
                            fclose(output) ;
                        }
    what i am trying to do is take the data that was read in from a file and stored, and write out to a different file. the data is stored in loadedSequences[nSequences].data and loadedSequences[nSequences].header

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's nothing wrong with the fputs calls, provided the output file is opened and list_num is valid and there is data in the .header and .data strings. Are you sure that all those are true?

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Thanks. My mistake was my index number. It should have been:

    Code:
    fputs(loadedSequences[list_num-1].header, output) ;
                            fputs(loadedSequences[list_num-1].data, output) ;

    Appreciate the help

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    typedef struct { char *name ; char data[1000000] ; int length ; int type ; char header[1000] ; } sequence ;
    
    sequence loadedSequences[1000] ;
    sizeof( loadedSequences ) is well over 1GB, you really need a different approach which involves say allocating data at run-time.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing my com port, writing and reading data
    By shoobsie in forum C Programming
    Replies: 7
    Last Post: 09-16-2005, 03:29 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Trouble writing to file
    By Fyodorox in forum C++ Programming
    Replies: 7
    Last Post: 05-06-2002, 06:09 PM
  5. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 01:45 PM