#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    How to print a string then print another string in a newline in a text file?


    Well basically the title says it all what I would want to do:In a text file, print a string then new line then print another string. i tried this:
    Code:
    FILE *fp;
    fp=fopen("txts/database.txt","a+");
    fputs(boy_encrypt,fp);
    fprintf(fp,"\n");
    fclose(fp);
    	
    fp=fopen("txts/database.txt","a+");
    fputs(girl_encrypt,fp);
    fprintf(fp,"\n");
    fclose(fp);
    But what it does is that it keeps on printing strings simultaneously, without any spaces at all.
    What to do with it?
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    I am not sure what you mean by " printing strings simultaneously"; they are definitely output sequentially.

    It is hard to tell why that would not work, and I suspect that your problem is in fact elsewhere. The code does not check that the file is opened successfully, and we can only guess what boy_encrypt and girl_encrypt might contain. Have you stepped through this code in your debugger to see what it actually does?

    I have to wonder why you might close and reopen the file between each line and why you fell the need to use fputs() and fprintf(). Why not just:

    Code:
    FILE* fp ;
    fp = fopen( "txts/database.txt", "a+" ) ;
    if( fp != NULL )
    {
        fprintf( fp, "%s\n", boy_encrypt ) ;
        fprintf( fp, "%s\n", girl_encrypt ) ;
        fclose( fp ) ;
    }
    Last edited by clifford; March 22nd, 2013 at 12:23 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by clifford
    I am not sure what you mean by " printing strings simultaneously"; they are definitely output sequentially.

    It is hard to tell why that would not work, and I suspect that your problem is in fact elsewhere. The code does not check that the file is opened successfully, and we can only guess what boy_encrypt and girl_encrypt might contain. Have you stepped through this code in your debugger to see what it actually does?

    I have to wonder why you might close and reopen the file between each line and why you fell the need to use fputs() and fprintf(). Why not just:

    Code:
    FILE* fp ;
    fp = fopen( "txts/database.txt", "a+" ) ;
    if( fp != NULL )
    {
        fprintf( fp, "%s\n", boy_encrypt ) ;
        fprintf( fp, "%s\n", girl_encrypt ) ;
        fclose( fp ) ;
    }
    I tried it out...it did not work.what's printed in the text file is like this:
    ted mosbyrobin scherbatsky

    when I actually wanted it to be printed this way:
    ted mosby
    robin scherbatsky


    this is I THINK the important part of my whole code:
    Code:
     
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    main()
     {
    int i;	
    
    char boy_encrypt[100];
    char girl_encrypt[100];
    
    char girl[100];
    char boy[100];
    
    gets(boy);
    	printf("\nWhat is the boy's name?");
    	gets(boy);
    	b=strlen(boy);
    	printf("\nWhat is the girl's name?");
    	gets(girl);
    	strcpy(boy_encrypt,boy);
    	strcpy(girl_encrypt,girl);
    	g=strlen(girl);	
    FILE *fp;
    	fp=fopen("txts/database.txt","a+");
    	fprintf( fp, "%s\n", boy_encrypt ) ;
            fprintf( fp, "%s\n", girl_encrypt ) ;
            fclose( fp ) ;
    	printf("Successfully stored\n");
     }//end of FUNCTION store()
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    This code should append 2 lines to your database.

    Do not use gets. fgets works instead. Then take some pains to remove a possible new line for equivalent functionality.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    char*prompt(char*message, char*buffer, int length) {
      int i;
      fputs(message,stdout);
      buffer[0] = 0;
      fgets(buffer,length,stdin);
      i = strlen(buffer);
      if (i && ('\n' == buffer[i-1]))
        buffer[i-1] = 0;
      return buffer;
    }
    
    int main() {
      FILE *fp;
      char boy_encrypt[100];
      char girl_encrypt[100];
      char girl[100];
      char boy[100];
      prompt("\nWhat is the boy's name?", boy, sizeof(boy));
      prompt("\nWhat is the girl's name?", girl, sizeof(girl));
      strcpy(boy_encrypt,boy);
      strcpy(girl_encrypt,girl);
      fp=fopen("txts/database.txt","a+");
      fprintf(fp, "%s\n", boy_encrypt);
      fprintf(fp, "%s\n", girl_encrypt);
      fclose(fp);
      puts("Successfully stored");
      return 0;
    }
    Last edited by b49P23TIvg; March 22nd, 2013 at 02:08 PM. Reason: Insert expectation.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo