Thread: saving to a text file

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    saving to a text file

    i have been given a txt input file and inside of it i am given the name of what the save file needs to be called without me writing it in the code myself. also i need the output of the file to be saved in this output file

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Blonde!

    First, I'd have to see the file, and how your program uses it, to answer your questions.

    Second, I'd have to see that you're working on this. Both cases require you to post your code - and the file. Maybe not all of it, but the relevant parts.

    You can't usually diagnose the problems, without seeing the patient.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I think your question has to do with hard-coding the filename vs. using a buffer that gets the file name from elsewhere.

    Once you read that part of the file which contains the file name, it's in a char buffer.
    You can then fopen(buffer ... to create the new file.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    #define COLS 80
    #define ROWS 23
    
    
    /* Function prototypes */
    int init(char);
    int display();
    int drawPt(char, int, int);
    int drawRect(char, int, int, int, int);
    int drawLine(char, int, int, int, int);
    int fill(char, int, int);
    int saveFile(char* filename);
    char buffer [80];
    FILE *input, *output;
    /* Global variables */
    char screen[COLS][ROWS];
    
    
    int main(void){
      input= fopen("finalinput.txt", "r");
    
    int x1, x2, y1, y2;
    char *cmd, *outChar;
    while ( fgets(buffer, 80, input) != NULL){
      /*making sure all coords are initialized*/
      x1 = 0;
      x2 = 0;
      y1 = 0;
      y2 = 0;
      /*grabbing a line and splitting it into peices*/
      cmd=strtok(buffer,",");
      outChar= strtok(NULL, ",");
      x1= atoi(strtok(NULL, ","));
      y1= atoi( strtok(NULL, ","));
      x2= atoi( strtok(NULL, ","));
      y2= atoi(strtok(NULL, ","));
      /*taking the line and sending it to the
      function it needs to be sent to*/
      switch (cmd[0]){
        case 'I':
          init( outChar[0]);
          break;
        case 'D':
          display();
          break;
        case 'R':
          drawRect(outChar[0],x1, y1, x2, y2);
          break;
        case 'P':
          drawPt(outChar[0],x1, y1);
          break;
        case 'L':
          drawLine(outChar[0],x1, y1, x2, y2);
          break;
        case 'F':
          fill(outChar[0],x1, y1);
          break;
        case 'S':
          saveFile(outChar);
          break;
        default:
          break;
      }
    
    }
        system("PAUSE");
        return 0;
    }
    
    /**************************************************************/
    /* init: Clears the screen by assigning all pixels the passed */
    /* character. Receives a character to use in clearing the     */
    /* screen.                                                    */
    /* Success returns a 0.                                       */
    /**************************************************************/
    int init(char p) {
        int x, y;
        for (x = 0; x < COLS; x++) {
            for (y = 0; y < ROWS; y++) {
                screen[x][y] = p;
            }
        }
    
        return 0;
    }
    /**************************************************************/
    /* display: Prints the screen matrix to the console.          */
    /* Success returns a 0.                                       */
    /**************************************************************/
    int display() {
        int x, y;
    
        /*The following line just puts a border above the screen    */
        for (x = 0; x < COLS; x++)
            putchar('_');
    
        /*Print out the screen matrix to standard output            */
        for (y = ROWS - 1; y >= 0; y--) {
            for (x = 0; x < COLS; x++) {
                putchar(screen[x][y]);
            }
        }
    
        /*The following line just puts a border below the screen.   */
        for (x = 0; x < COLS; x++)
            putchar('_');
        return 0;
    }
    
    /**************************************************************/
    /* drawPt: draws a point containing a border character using  */
    /* coordinates x,y.                                           */
    /* Returns -1 if the coordinates are out of bounds and 0 for  */
    /* success.                                                   */
    /**************************************************************/
    int drawPt(char b, int x, int y) {
        /*Test for out of bounds */
        if (x < 0 || x >= COLS) return -1;
        if (y < 0 || y >= COLS) return -1;
    
        /*Otherwise, draw the point */
        screen[x][y] = (char) b;
        return 0;
    }
    
    /**************************************************************/
    /* drawRect: draws a rectangle with a border character using  */
    /* coordinates x1,y1 and x2,y2 to form opposite corners.      */
    /* Returns -1 if the coordinates are out of bounds and 0 for  */
    /* success.                                                   */
    /**************************************************************/
    int drawRect(char b, int x1, int y1, int x2, int y2) {
    
        /*Test for out of bounds*/
        if (x1 < 0 || x1 >= COLS) return -1;
        if (x2 < 0 || x2 >= COLS) return -1;
        if (y1 < 0 || y1 >= ROWS) return -1;
        if (y2 < 0 || y2 >= ROWS) return -1;
    
        int c = abs(x1 - x2); /* temporary counter variables */
        int r = abs(y1 - y2); /* temporary counter variables */
        int x = x1 >= x2 ? x2 : x1; /* Set x to the lowest coord.  */
        int y = y1 >= y2 ? y2 : y1; /* Set y to the lowest coord.  */
    
        while (c >= 0) { /* Draw the horizontal lines.  */
            screen[x + c][y1] = b;
            screen[x + c][y2] = b;
            c--;
        }
    
        while (r >= 0) { /* Draw the vertical lines.    */
            screen[x1][y + r] = b;
            screen[x2][y + r] = b;
            r--;
        }
    
        return 0;
    }
    
    /**************************************************************/
    /* drawLine: draws a line with a border character using       */
    /* coordinates x1,y1 and x2,y2 to form opposite ends   .      */
    /* A line can be expressed in the form y = mx + b             */
    /* Returns -1 if the coordinates are out of bounds and 0 for  */
    /* success.                                                   */
    /**************************************************************/
    int drawLine(char line_char, int x1, int y1, int x2, int y2){
    int i, j, x, y, xlow, ylow, xhigh, yhigh, z;
    double m=0;
    
        /*making sure coords given are inside perimeters*/
        if (x1 < 0 || x1 >= COLS) return -1;
        if (x2 < 0 || x2 >= COLS) return -1;
        if (y1 < 0 || y1 >= ROWS) return -1;
        if (y2 < 0 || y2 >= ROWS) return -1;
        
      /*declaring xlow, xhigh, ylow, and yhigh*/
      if (x1>x2){
        xlow=x2;
        xhigh=x1;
      }
        else{
          xlow=x1;
          xhigh=x2;
      }
      if (y1>y2){
        ylow=y2;
        yhigh=y1;
      }
        else{
          ylow=y1;
          yhigh=y2;
      }
      /*draws horizontal and vertical lines*/
      if ((x1-x2)==0){
        for(j=ylow; j<=yhigh; j++) {
          screen[x1][j]=line_char;
        }
        return 0;
      }
      if ((y1-y2)==0){
        for(i=xlow; i<=xhigh; i++){
          screen[i][y1]=line_char;
        }
        return 0;
      }
      /*finds slope*/
      m=(double)(y2-y1)/(x2-x1);
      
      /*draws sloped lines*/
      if (fabs(m)>1){
        for (j=ylow; j<=yhigh; j++){
          x= (int) (((1/m)*(j-y1))+x1+0.5);
          screen[x][j]=line_char;
        }
      return 0;
      }
      if (fabs(m)<=1){
        for (i=xlow; i<=xhigh; i++){
          y= (int) ((m*(i-x1))+y1+0.5);
          screen[i][y]=line_char;
        }
        return 0;
      }
    
        return 0;
    
    }
    /**************************************************************/
    /* fill: fills an area with a supplied fill character,        */
    /* replacing all empty spaces (those pixels with a space      */
    /* character in a horizontal or vertical direction.           */
    /* This function uses recursion.                              */
    /* Returns -1 if the coordinates are out of bounds, 1 if the  */
    /* coordinates are not a space and 0 for success.             */
    /**************************************************************/
    
    int fill(char fill_char, int x, int y) {
          /*out of bounds test*/
        if (x < 0 || x >= COLS) return -1;
        if (y < 0 || y >= ROWS) return -1;
      /*filling all around coord given*/
      if(screen[x][y]==' '){
        screen[x][y]=fill_char;
        fill(fill_char,x-1,y);
        fill(fill_char,x+1,y);
        fill(fill_char,x,y-1);
        fill(fill_char,x,y+1);
          return 0;
      }
      else return 1;
    }
    /**************************************************************/
    /* saveFile: Prints the screen matrix to a file.              */
    /* Success returns a 0.                                       */
    /**************************************************************/
    int saveFile(char *filename){
        
        return 0;
    }
    this is the whole code but the only problem is with the save function at the very bottom

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A few notes:

    char * filename isn't referenced anywhere else, except as a parameter for this function.
    So I'd be tempted to add this line of code in main():
    :
    Code:
    input= fopen("finalinput.txt", "r");  //already exists
    output = fopen("finaloutput.txt", "w"); //so add this line
    Then change your parameters for saveFile to:
    int saveFile(FILE *);
    your call to:
    saveFile(output);
    and your saveFile to:
    int saveFile(FILE *output)

    then do code very similar to:
    Code:
        /*Print out the screen matrix to the output file */
        for (y = ROWS - 1; y >= 0; y--) {
            for (x = 0; x < COLS; x++) {
                fputc(screen[x][y], output);
            }
        }
    
    and add:
    fclose(output);
    at the end of the function.
    Good idea to test your input and output file pointers, before you leave main().
    Last edited by Adak; 11-13-2010 at 02:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with searching text file
    By zacharyrs in forum C Programming
    Replies: 30
    Last Post: 12-01-2009, 03:13 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Function is called and I am trying to open a file
    By tommy69 in forum C Programming
    Replies: 88
    Last Post: 05-06-2004, 08:33 AM