Thread: Add a char in front of a few string

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    29

    Add a char in front of a few string

    This is my first time sending any things using a cell phone. My fingers don't fit.

    I have a ton of numbers that i am trying to make changes to.

    Acct.txt
    89896
    7874
    90000
    9998
    80234

    I need to insert a zero in front of every number which only have four digits.

    AcctNew.txt
    89896
    07874
    90000
    09998
    80234

    This is the best I could do. All else I tried is even more embarrassing. Ninety thousand is the highest number.

    Code:
    #define MAX_LINE 90000
    
    int main() {
    
    FILE *fptr, *fptw;
    int i;
    int count, nc=0, sum=0;
    
    char zero[] = "0";
    char destination[6];
    
    ftpr=fopen("Acct.txt","r");
    ftpr=fopen("Acct New.txt","w");
    
    int lines = 1;
    char line[MAX_LINE];
    
    
    while ( fgets(line,sizeof line,)!=NULL )
       {
            line[strlen(line) -1] = 0;
            printf("Acct: :%s\t", line);
            print f("%d = %lu\n", lines,strlen(line));
    
    lines += 1;
       }
    
    fclose(fptr);
    fclose(fptw);

  2. #2
    Registered User
    Join Date
    Jun 2019
    Posts
    29

    my other part:

    I just can't figure what is missing or how to fit it in.
    Code:
    int i = 8;
    char line = i + '0';
    
    if(line < 5)
            {
            fputs(destination, fptw);
            }
    else {
             strcpy(destination, zero);
             strcat(destination, &line);
             fputs(destination, fptw);
             memset(destination, 0x00, sizeof(destination));
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,943
    Are you aware that the format specifier for printf has the notion of a field and field width, as well as fill character? You can just specify that the integer is to be printed with a field width of 5, to be filled with leading 0s.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2019
    Posts
    29
    I was unaware that format specifiers could do all of that. I'm reading Alex's Printf Format Strings already. Between that and pointers I'm going to study well. Thanks so much @laserlight.

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    29
    I think it's the way the terminals works. They align data to the left. So when I add zero to the front every single line gets one. I threw every thing Alex had at it. It workd for char becaisr yoi can make adjustment. Every thing else works so I'll be using it for other stuff, but for now its back to the drawing board.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,943
    Quote Originally Posted by jc2020
    I think it's the way the terminals works. They align data to the left. So when I add zero to the front every single line gets one. I threw every thing Alex had at it. It workd for char becaisr yoi can make adjustment. Every thing else works so I'll be using it for other stuff, but for now its back to the drawing board.
    No, it has nothing to do with terminals. You're just doing it wrong. For example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        unsigned int numbers[] = {0, 1, 12, 123, 1234, 12345};
        for (size_t i = 0; i < sizeof(numbers) / sizeof(numbers[0]); ++i)
        {
            printf("%05u\n", numbers[i]);
        }
        return 0;
    }
    Notice that the input must be integers, i.e., you read the numbers as integers, not strings. If you prefer or must work with strings (e.g., because the data isn't necessarily numeric), then this approach doesn't work, but the alternative is so simple:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char numbers[][6] = {"0", "1", "12", "123", "1234", "12345"};
        for (size_t i = 0; i < sizeof(numbers) / sizeof(numbers[0]); ++i)
        {
            size_t len = strlen(numbers[i]);
            // Zero pad if needed:
            if (len < 5)
            {
                for (size_t j = 0; j < (5 - len); ++j)
                {
                    putchar('0');
                }
            }
            // Print the value itself:
            printf("%s\n", numbers[i]);
        }
        return 0;
    }
    Once you have this down pat, you can then do other things like add other formatting as needed. The problem seems to be that you dived into something complex instead of putting your focus on something simple first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    16

    Post This might help...

    zeros.c
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define FILE_SIZE_MAX 10000
    #define NUMBER_SIZE_MAX 10
    
    #define FILE_IMPORT "acct.txt"
    #define FILE_EXPORT "acctnew.txt"
    
    char importData[FILE_SIZE_MAX]; 
    char exportData[FILE_SIZE_MAX]; 
    char tmpData[NUMBER_SIZE_MAX];
    char tmp[NUMBER_SIZE_MAX];
    
    void import() {    
        memset(importData,0,sizeof(importData));
        memset(tmpData,0,sizeof(tmpData));
        FILE* file = fopen(FILE_IMPORT, "r");    
        while (fgets(tmpData, sizeof(tmpData), file)) {
            strcat(importData, tmpData); 
        };
        fclose(file);
    };
    
    char *fixNumber() {
        memset(tmp,0,sizeof(tmp));
        int addZeros = 5 - strlen(tmpData);    
        for (int i = 0; i < addZeros; i++) {
            strcat(tmp,"0");
        };
        strcat(tmp,tmpData);    
        memset(tmpData,0,sizeof(tmpData));
        return tmp;
    };
    
    int main(int argc, char *argv[]) {      
    
        tmpData[0] = '\0'; 
        import(); int i; int count = 0;
        
        remove(FILE_EXPORT);
        FILE *exportFile = fopen(FILE_EXPORT, "a");
    
        for (i = 0; i < strlen(importData); i++ ) {        
            if (importData[i] == '\n') {                        
                if (count <= 4 && count != 0) {                                                   
                    fprintf(exportFile, "%s\n", fixNumber());
                } else {                
                    fprintf(exportFile, "%s\n", tmpData);
                };
                count = 0;
            } else {
                tmpData[count] = importData[i];
                count++;
            };
        };
    
        fclose(exportFile);
    
    };
    It is flawed and i threw it together really quick but i'm assuming it will help you on the way to a final product.
    Last edited by Structure; 1 Week Ago at 11:27 AM.


  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,850
    I'd be tempted to read the number into an integer (fscanf/%d), and then use fprintf with a format specifier (%05s)
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    618
    Stucture's "solution" is total garbage.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rejecting float and string with int on the front
    By Pole in forum C Programming
    Replies: 3
    Last Post: 06-01-2012, 01:02 PM
  2. Add to Front, Remove from Front, Traverse
    By Brandon Smith in forum C Programming
    Replies: 9
    Last Post: 04-10-2012, 03:26 PM
  3. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  4. Replies: 3
    Last Post: 04-06-2005, 11:04 AM
  5. Appending to the front of a string in a class...
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 11-26-2002, 05:52 PM

Tags for this Thread