Thread: Newline problem

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Fort Worth TX
    Posts
    15

    Newline problem

    I need help i cant figure out how to delete this newline without deleting it from the file if you know what i mean heres my code the problem is in the while loop.Because if i delete the newline right after fgets in the while loop all the text in the file will be horizontal no newlines.so yeah please help.


    Code:
    int createfile() {
    
    
        FILE *f;
    
        char filename[256];
        char filetext[2048];
    
    
        printf("File Name:");
        scanf("%s",filename);
        printf("[%s]",filename);
        if((f=fopen(filename,"w")) == NULL) {
           printf("Cannot open file %s.\n",filename);
           return 0;
        }
        printf("File %s opened for writing.",filename);
    
        printf("\n------------------------------------\n");
    
        while(1) {
    
           fgets(filetext,2048,stdin);
           printf("[%s]",filetext);
           if(strstr(filetext,"~")) {
              filetext[strlen(filetext) -2] = '\0';
              printf("[%s]",filetext);
              fprintf(f,"%s",filetext);
              break;
           }
           fprintf(f,"%s",filetext);
        }
    
        printf("File %s was created.\n",filename);
        fclose(f);
    
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Check if there is a newline in the buiffer received with fgets(). Strip the newline if so. When you print it, if you had to strip it, then print an extra newline to the file.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Location
    Fort Worth TX
    Posts
    15
    Should i do it like this?

    Code:
    while(1) {
    
           fgets(filetext,2048,stdin);
           filetext[strlen(filetext) -1] = '\0';
           printf("[%s]",filetext);
           if(strstr(filetext,"~")) {
              filetext[strlen(filetext) -2] = '\0';
              printf("[%s]",filetext);
              fprintf(f,"%s",filetext);
              break;
           }
           fprintf(f,"\n",filetext);
           fprintf(f,"%s",filetext);
        }

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Why are you complicating this and doing it illogically?

    Code:
    fprintf(f,"%s\n",filetext);
    Strip the newline with strchr(). Something like this (untested, btw, from memory):

    Code:
    char *ptr;
    
    ...
    
    ptr = strchr(filetext, '\n');
    if(ptr)
    {
        *ptr = '\0';
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would think you would want to print the data first, then the newline.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Location
    PA, USA
    Posts
    11
    The following code also works

    Code:
    int createfile() {
        FILE *f;
    
        char filename[256];
        char filetext[2048];
    
        printf("File Name:");
        scanf("%s",filename);
        printf("[%s]",filename);
        if((f=fopen(filename,"w")) == NULL)
        {
            printf("Cannot open file %s.\n",filename);
            return 0;
        }
        printf("File %s opened for writing.",filename);
    
        printf("\n------------------------------------\n");
    
        while(1)
        {
            fgets(filetext,2048,stdin);
            filetext[strlen(filetext) -1] = '\0';
            printf("[%s]",filetext);
            filetext[strlen(filetext)] = '\n';
            if(strstr(filetext,"~"))
            {
                printf("[%s]",filetext);
                fprintf(f,"%s",filetext);
                break;
            }
            fprintf(f,"%s",filetext);
        }
    
        printf("File %s was created.\n",filename);
        fclose(f);
    
    }

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    PA, USA
    Posts
    11
    By mistake I preserved the ~ character in the file. Corrected in the following

    Code:
    int createfile() {
        FILE *f;
    
        char filename[256];
        char filetext[2048];
    
        printf("File Name:");
        scanf("%s",filename);
        printf("[%s]",filename);
        if((f=fopen(filename,"w")) == NULL)
        {
            printf("Cannot open file %s.\n",filename);
            return 0;
        }
        printf("File %s opened for writing.",filename);
    
        printf("\n------------------------------------\n");
    
        while(1)
        {
            fgets(filetext,2048,stdin);
            filetext[strlen(filetext) -1] = '\0';
            printf("[%s]",filetext);
            filetext[strlen(filetext)] = '\n';
            if(strstr(filetext,"~"))
            {
                filetext[strlen(filetext) -2] = '\0';
                fprintf(f,"%s",filetext);
                break;
            }
            fprintf(f,"%s",filetext);
        }
    
        printf("File %s was created.\n",filename);
        fclose(f);
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Cin newline space problem... or something
    By Baqualish in forum C++ Programming
    Replies: 10
    Last Post: 10-17-2007, 03:49 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM