Thread: Convert list of names from one text file to email format in a new file

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    5

    Convert list of names from one text file to email format in a new file

    Hello everyone,

    I'm a total self-taught (so far) newbie here, so please bear with me. I have almost a hundred names in a text file that I want to convert to email addresses and save to another file. I seem to have it working, but it doesn't print the full names in the email prefix. The output I'm looking for is Doe_John@livebrandm, but I'm only getting D_J@livebrandm. I'm not sure what I should specifically be reading up on that applies to this directly. I would appreciate any help with this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        
        FILE *fpin=fopen("namesIN.txt", "r"); 
        FILE *fpout=fopen("emailOUT.txt", "a");
        char first[20],last[20],inbuff[1500];
        
        if((fpin=fopen("namesIN.txt","r"))==NULL)
        {
            printf("\nCannot open input file");
            exit(1);
        }
        if((fpout=fopen("emailOUT.txt","a"))==NULL)
        {
            printf("\nCannot open output file.txt");
            exit(2);                
        }                    
    
    
        while(fgets(inbuff,1500,fpin) !=NULL)
        {
            sscanf(inbuff,"%S%S",first,last);
            printf("%s_%[email protected]\n",last,first);
            fprintf(fpout,"%s_%[email protected]\n",last,first);
        }
        
        fclose(fpin);
        fclose(fpout);
        return 0;
        }
    namesIN.txt

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Could it be because you're using a capital S in the sscanf conversion specifier? (I've never seen that before.)


    Also note that you're opening your files twice!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Try changing your sscanf() specifiers to "%s", "%S" is not a valid standard C specifier.

    Jim

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Aha! Finally I have Colonel Sanders' email address!

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Apparently a capital S is an extension (in gcc at least) that is equivalent to the standard %ls, which expects a wchar_t pointer.


    This will actually read the entire name, but store it (on my machine at least) as the ascii value of the first letter followed by a zero byte, then the ascii value of the second letter followed by a zero byte, etc., and finally two zero bytes (a wchar_t null character). So when you print it with a %s specifier, the first zero byte is interpretted as the end sentinel. Hence you end up with just the first letter.


    Turning up the warning level on your compiler may warn you of the mismatch between the specifier and the proferred pointer. gcc gives such a warning with the -Wall flag.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    @oogabooga Thanks a lot for the quick reply. It was right under my nose and I just couldn't see it. I guess that'll get easier as I get better at this. Thanks for the file opening heads up too. You guys are awesome!

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Quote Originally Posted by oogabooga View Post
    Apparently a capital S is an extension (in gcc at least) that is equivalent to the standard %ls, which expects a wchar_t pointer.


    This will actually read the entire name, but store it (on my machine at least) as the ascii value of the first letter followed by a zero byte, then the ascii value of the second letter followed by a zero byte, etc., and finally two zero bytes (a wchar_t null character). So when you print it with a %s specifier, the first zero byte is interpretted as the end sentinel. Hence you end up with just the first letter.


    Turning up the warning level on your compiler may warn you of the mismatch between the specifier and the proferred pointer. gcc gives such a warning with the -Wall flag.

    I'll be saving this post so that when I become a real coder I can read it && understand it. lol

  8. #8
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Hey again, folks. I tried to make what I thought would be a simple tweak to the output in this program, but I'm having a little problem. Instead of printing the full first name in the prefix of the emails, I wanted to change it to just the first initial. Basically I want to change the output from doe_John@livebrandm to doe_j@livebrandm. I changed the second %s to %c in printf and fprintf on lines 27 and 28:

    Code:
    printf("%s_%[email protected]\n",last,first);
    fprintf(fpout,"%s_%[email protected]\n",last,first);


    and now the output just reads: doe_@@livebrandm, giving me a second @ symbol rather than a first initial. The %c and adding a "system("pause") at the end were the only changes I made. Can someone point out where I'm screwing up at please?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    first isn't a character, so can't be printed using %c. If you wanted a particular char out of that char array, well, that's why there's [array notation].

  10. #10
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Quote Originally Posted by tabstop View Post
    first isn't a character, so can't be printed using %c. If you wanted a particular char out of that char array, well, that's why there's [array notation].

    Thank a lot. I hadn't known about array and pointer notation yet. I looked them up real quick and got things working again with the newly desired output. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. to convert a .hdf file to a text file
    By venkat_gummadi in forum Tech Board
    Replies: 3
    Last Post: 10-09-2006, 04:16 PM
  2. traverse a directory and list all file names?
    By George2 in forum C Programming
    Replies: 3
    Last Post: 08-24-2006, 06:18 PM
  3. is there a way to write the file names in a folder in a text file?
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-15-2002, 05:11 PM
  4. Convert a text file to a binary file
    By Cyber Kitten in forum C Programming
    Replies: 16
    Last Post: 02-04-2002, 08:53 AM