how can i save the output of this function to use later?

This is a discussion on how can i save the output of this function to use later? within the C Programming forums, part of the General Programming Boards category; Hey there, I've been working on this problem for a while, using "good" programming techniques by breaking the problem up ...

  1. #1
    Unregistered
    Guest

    how can i save the output of this function to use later?

    Hey there,
    I've been working on this problem for a while, using "good" programming techniques by breaking the problem up into smaller chunks and working on them. You guys have been real helpful so far, now I'm a couple of steps away from finishing my code, I think. I've been able, through a lot of help from you guys, to hard code in a string of IntelHEX code and perform operations on that, now I'd like to move onto the actual file itself. So, as of now, I'm able to open up the file and have it print out onto my output screen. From here, I need to be able to save that output into a character array so I can then perform my other subroutines and break down this code into useful data...useful for humans, I should say.

    Code:
    typedef void (*ReadLineFunc)(const char *);
    
    int ReadLines(const char *Filename, ReadLineFunc Callback) 
    {
      FILE *fp = fopen("filename", "r");
      char	hex2[512];
      int	m=1;
      
      if(fp == NULL) {
        fprintf(stderr,"Error, file is not found. \n");
        return -1;
      }
    
      /* Read each line and print it out */
      for(m=1;m<40;m=m+1) 
      {
        if(fgets(hex2, 512, fp) == NULL) {
          /* No more lines to read */
          break;
        }
    
        Callback(hex2);
      }
    
      fclose(fp);
    
      return 0;
    }
    
    
    void MyReadLineFunc(const char *Line) 
    {
      /* Prints out all except the first character */
    
      if(Line[0])
        puts(&Line[1]);
    }
    (hope I got that code posting thingee right too!!) Anyway, that's that problem. If you guys could help me out, I'd appreciate it. I was thinking of using strcpy, but don't know exactly how to use it. hex2 is the char array I've been using in my other routines to store in the hard coded input string.

    Cheers

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    strcpy(char * destination, char *sourceString);
    PHP and XML
    Let's talk about SAX

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Why do you need to copy the string? Since the file remains unchanged there's no worry of corrupting the data, you can simply process the input string. This would save you quite a bit in memory usage and processing time because you wouldn't have to waste the memory for saving a copy of the string and you wouldn't have to wait for it to be copied. There are situations where a copy helps, such as with strtok, in which case you would have to consider whether or not each line is the same length or if it varies.

    If the length varies then you would be more space efficient by dynamically allocating just enough space for each line, otherwise another static array of 512 would work fine:
    Code:
    int ReadLines(const char *Filename, ReadLineFunc Callback) 
    {
      FILE *fp = fopen(Filename, "r");
      char	hex2[512];
    #define VARIED_LENGTH_STRINGS
    #ifdef VARIED_LENGTH_STRINGS
      char *copy;
    #else
      char  copy[512];
    #endif
      
      if(fp == NULL) {
        fprintf(stderr,"Error, file is not found. \n");
        return -1;
      }
    
      /* Read each line and print it out */
      while(fgets(hex2, 512, fp) != NULL) {
    #ifdef VARIED_LENGTH_STRINGS
        copy = malloc ( strlen ( hex2 ) + 1 );
        if ( copy != NULL ) {
          Callback(copy);
          free ( copy );
          copy = NULL;
        }
    #else
        strcpy ( copy, hex2 );
        Callback(copy);
    #endif
      }
    
      fclose(fp);
    
      return 0;
    }
    -Prelude.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    I'm not sure what you mean by saving it. You are saving it in a buffer as it is, and you could do all thw work you need within the loop. But I do have a suggestion for you - drop the for loop and use a while loop instead of your if statement.

    >> while( fgets(hex2, 512, fp) != NULL) )
    >> {
    >> /* work you want done */
    >> )


    That way you keep reading until the end of file and don't have to guess at the size.


    EDIT: Damn, prelude answered while I was typing
    Last edited by drharv; 07-18-2002 at 09:32 AM.

  5. #5
    Unregistered
    Guest
    Prelude,
    what do the '#' symbols represent in your code?

    drharv,
    by saving it, I meant to say, how can I pass this string to another function so I can manipulate it further?

    Thanks in advance!!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    That's conditional compilation, if the line

    #define VARIED_LENGTH_STRINGS

    Is there then the dynamic allocation method is used, otherwise the static array method is used. It saves me the trouble of posting the function twice. The idea is the same as an if statement.
    Code:
    /* If VARIED_LENGTH_STRINGS is defined */
    #ifdef VARIED_LENGTH_STRINGS
      /* Do this */
      char *copy;
    /* If VARIED_LENGTH_STRINGS is not defined */
    #else
      /* Do this */
      char  copy[512];
    /* End conditional compilation */
    #endif
    -Prelude
    My best code is written with the delete key.

  7. #7
    Unregistered
    Guest
    Prelude, when I utilize your code, I get a Link Error:
    unresolved external symbol _MyReadLineFunc

    please advise....

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Show your code. It appears as if you are trying to use MyReadLineFunc but you don't define it anywhere.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Unregistered
    Guest
    the code is extensive... here are the include files and the function prototypes:

    Code:
    #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     #include "genlib.h"
     #include "strlib.h"
     #include "simpio.h"
    
    FILE	*zz;
    
    /* Function prototypes */
    
    static int CHECKSUM (char *buffer, int length, int mode, int seed, int *result);
    static int INTHEX (char *hex, int addr1, int addr2, int fill, char *buffer);
    static int ACCommunication_convert_value_to_int(const char *p_value);
    static int BCCommunication_convert_value_to_int(const char *p_value);
    static int CCCommunication_convert_value_to_int(const char *p_value);
    static int DCCommunication_convert_value_to_int(const char *p);
    void MyReadLineFunc(const char *Line);
    int func(char *p_value);

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >here are the include files and the function prototypes
    Make sure that you define the function somewhere and not just declare it. In your first post you gave two functions, but I removed one of them for the sake of brevity, don't forget to replace it if you cut and pasted the code I gave you.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Unregistered
    Guest
    First off, DOH!!! Second, the output file is now printing out junk and beautiful IntelHEX code went away :-(

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >the output file is now printing out junk and beautiful IntelHEX code went away
    Step through your code and make sure that your data doesn't get corrupted somewhere. Are you returning a local array or something else where memory is released before you get it?

    -Prelude
    My best code is written with the delete key.

  13. #13
    Unregistered
    Guest
    I'm not able to step through my function, it asks me to please enter the path for FOPEN.C
    and I can't get around that dialog box. So what I was wondering is if anybody would be willing to try and compile Prelude's code using an input file containing:
    :0E000A0040C0068450B0040188B00701BCC0A7

    and let me know what their compiler outputs.. i know it's asking a lot

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Heheh, silly me. I forgot to add code which would copy the contents of hex2 to the dynamically allocated copy.
    Code:
    int ReadLines(const char *Filename, ReadLineFunc Callback) 
    {
      FILE *fp = fopen(Filename, "r");
      char	hex2[512];
      char *copy;
      
      if(fp == NULL) {
        fprintf(stderr,"Error, file is not found. \n");
        return -1;
      }
    
      /* Read each line and print it out */
      while(fgets(hex2, 512, fp) != NULL) {
        copy = malloc ( strlen ( hex2 ) + 1 );
        if ( copy != NULL ) {
          strcpy ( copy, hex2 );
          Callback(copy);
          free ( copy );
          copy = NULL;
        }
      }
    
      fclose(fp);
    
      return 0;
    }
    >it asks me to please enter the path for FOPEN.C
    Step over calls to standard library functions, not into them. There should be a separate step command for that.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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