Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: C, fprintf with commas

  1. #11
    squakie is offline I Ubuntu, Therefore, I Am
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C, fprintf with commas

    Okay I did a dump of the input file:
    Code:
    dave@davePC:~/juno-address-book$ hexdump -C test-addrbook.nv
    00000000  54 79 70 65 3a 45 6e 74  72 79 0d 0a 45 6d 61 69  |Type:Entry..Emai|
    00000010  6c 3a 6d 79 65 6d 61 6c  40 6d 69 6e 65 2e 63 6f  |l:myemal@mine.co|
    00000020  6d 0d 0a 41 6c 69 61 73  3a 0d 0a 4e 61 6d 65 3a  |m..Alias:..Name:|
    00000030  6c 61 73 74 20 6e 61 6d  65 2c 20 66 69 72 73 74  |last name, first|
    00000040  20 6e 61 6d 65 0d 0a 50  72 69 6d 61 72 79 20 50  | name..Primary P|
    00000050  68 6f 6e 65 3a 6d 79 20  70 68 6f 6e 65 0d 0a 42  |hone:my phone..B|
    00000060  69 72 74 68 3a 6d 79 20  62 69 72 74 68 0d 0a 41  |irth:my birth..A|
    00000070  64 64 72 65 73 73 3a 6d  79 20 61 64 64 72 65 73  |ddress:my addres|
    00000080  73 0d 0a 44 65 6c 65 74  65 64 3a 30 0d 0a 54 69  |s..Deleted:0..Ti|
    00000090  6d 65 3a 31 33 30 30 34  34 36 30 35 39 0d 0a 0d  |me:1300446059...|
    000000a0  0a 0d 0a                                          |...|
    000000a3
    dave@davePC:~/juno-address-book$
    Obviously the input "lines" are terminated by newlines since they came from the editor.


    And a dump of the output file:
    Code:
    dave@davePC:~/juno-address-book$ hexdump -C test-addr-book.txt
    00000000  4e 61 6d 65 2c 45 6d 61  69 6c 41 64 64 72 65 73  |Name,EmailAddres|
    00000010  73 2c 46 69 72 73 74 4e  61 6d 65 2c 4c 61 73 74  |s,FirstName,Last|
    00000020  4e 61 6d 65 0a 6c 61 73  74 20 6e 61 6d 65 20 20  |Name.last name  |
    00000030  20 20 66 69 72 73 74 20  6e 61 6d 65 0d 0a 2c 6d  |  first name..,m|
    00000040  79 65 6d 61 6c 40 6d 69  6e 65 2e 63 6f 6d 0d 0a  |yemal@mine.com..|
    00000050  2c                                                |,|
    00000051
    dave@davePC:~/juno-address-book$
    There are newlines (the 0d 0a in the dump) at the end of the first name and the email address, indicating to me, since I use pretty much the same code to generate all the string, that somehow I am adding the newlines to the output. Since the 0d 0a are not '\0', they are included in the input string, so the length I calculate (wrk2 in the code) should actually be the strlen of the string minus 2. I'll be making those changes to my code. Someone mentioned this earlier and I should have dumped the files then so I could have verified it then.


    EDIT: made the changes in the code - IT WORK!!

    Thanks SO much to everyone!!
    Last edited by squakie; February 28th, 2014 at 11:32 PM.

  2. #12
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: C, fprintf with commas

    Blindly removing 2 is fraught with peril. You get this because the file comes from a Windows system, but any kind of edit on a Linux may replace the line ends with just '\n' (and maybe '\r' on OSX). Either make your code more careful, or run the "fromdos" command on the file when you import it, it will convert the line ends to the Linux convention.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  3. #13
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,277
    Distro
    Kubuntu

    Re: C, fprintf with commas

    Quote Originally Posted by squakie View Post
    EDIT: I just noticed, by doing some more testing, that all of the generated strings appear to be having the same problem, as can be witnessed by the email address, an apparently long string so it does a newline, followed by it's terminating comma. Perhaps I'm doing something wrong with the memcpy statements? My understanding was that it would only copy the number of characters (wrk3 in the code) from the input string to the output string. Since I memset each output string to all '\0' before doing the memcpy, I assumed i would end up with a string output after the memcpy - if memcpy just copies that number of character the "preloaded" '\0' should terminate the output right after the number of characters are moved. Least wise I hope so. There can't be any extra newlines in the input as I created it right in gedit.
    I understood from your earlier post that you are re-learning C. You should stick to simple string manipulator functions rather than fiddle with memcpy() or even print out individual characters using fputc().

    Make these functions your friends: strncpy(), sscanf(), fgets(), and strchr().

    Here's a program that ingests a simple data file (similar to the one you presented in the post that I'm quoting above), and prints to standard-out a CSV representation of the data.

    Code:
    #include <string.h>
    #include <stdio.h>
    
    void extract_data_str(FILE* file, char* data, int data_len);
    void extract_data_val(FILE* file, int* data);
    
    void get_data_str(char* line, char* data, int max_len);
    void get_data_val(char* line, int* data);
    
    void remove_newline(char* line);
    
    
    int main(int argc, char** argv)
    {
    	if (argc != 2)
    	{
    		fprintf(stderr, "Usage: %s <data file>\n", argv[0]);
    		return -1;
    	}
    
    	FILE* file = fopen(argv[1], "r");
    
    	if (file)
    	{
    		char type[80]  = {0};
    		char email[80] = {0};
    		char alias[80] = {0};
    		char name[80]  = {0};
    		char phone[80] = {0};
    		char birth[80] = {0};
    		char addr[80]  = {0};
    		int  deleted;
    		int  time;
    
    		extract_data_str(file, type,  sizeof(type));
    		extract_data_str(file, email, sizeof(email));
    		extract_data_str(file, alias, sizeof(alias));
    		extract_data_str(file, name,  sizeof(name));
    		extract_data_str(file, phone, sizeof(phone));
    		extract_data_str(file, birth, sizeof(birth));
    		extract_data_str(file, addr,  sizeof(addr));
    
    		extract_data_val(file, &deleted);
    		extract_data_val(file, &time);
    
    		fprintf(stdout, "%s,%s,%s,%s,%s,%s,%s,%d,%d\n",
    		        type, email, alias, name, phone, birth, addr, deleted, time);
    
    		fclose(file);
    	}
    	else
    	{
    		fprintf(stderr, "Cannot open file %s.\n", argv[1]);
    		return -1;
    	}
    	return 0;
    }
    
    void extract_data_str(FILE* file, char* data, int data_len)
    {
    	char line[data_len * 2];
    	fgets(line, sizeof(line), file);
    	remove_newline(line);
    	get_data_str(line, data, data_len);
    
    }
    
    void extract_data_val(FILE* file, int* data)
    {
    	char line[64];
    	fgets(line, sizeof(line), file);
    	remove_newline(line);
    	get_data_val(line, data);
    }
    
    void get_data_str(char* line, char* str, int max_len)
    {
    	char* colon = strchr(line, ':');
    	if (colon)
    	{
    		strncpy(str, colon + 1, max_len);
    	}
    	else
    	{
    		strncpy(str, line, max_len);
    	}
    }
    
    void get_data_val(char* line, int* val)
    {
    	char* colon = strchr(line, ':');
    	if (colon)
    	{
    		sscanf(colon + 1, "%d", val);
    	}
    	else
    	{
    		sscanf(line, "%d", val);
    	}
    }
    
    void remove_newline(char* line)
    {
    	char* nl = strchr(line, '\n');
    	if (nl)
    	{
    		*nl = '\0';
    	}
    }
    Note: The program above assumes that if there are newlines in the file, that they are Linux-style newlines.
    Last edited by dwhitney67; March 1st, 2014 at 12:02 AM.

  4. #14
    squakie is offline I Ubuntu, Therefore, I Am
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C, fprintf with commas

    Well, I'll have to modify some code so I check for all 4 posdibilities at the end and back up accordingly.

    As far as memcpy goes, I didn't use strncpy as what I read on the net sounded like it didn't null terminate the string. So first I initialize the string to all nulls (at least I'm assuming that's what I'm doing), then when I used the memcpy I know what ever I copy in will be followed by a null, so there will be a string terminate, and so I can then access the output as a string.

    It *was* working just fine. Then I tried to add an fprintf in place of my output code, and everything went "wacky". I'm getting so many errors in compilation now that don't even make sense - it's saying things about the code that aren't there, things like multiple definitions of a string or function. So now I really don't understand what I've done. I've got to go back and reset all the code I *think* I changed. I was relying on the backup file from gedit (the ~ file), but I forgot to save a copy somewhere along the line before starting the changes so now I don't have a copy before all the changes. I'm stupid.

  5. #15
    squakie is offline I Ubuntu, Therefore, I Am
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C, fprintf with commas

    I'm just trying to read the file one "line" at a time, parse it, format the output strings, then write out a record.

    At first I wasn't sure if "my_input_string" is actually null terminated, so I used the "move this many characters" thing via memcpy and made sure it was null terminated after.

    I'm starting to think now that it might be null terminated, which would make my life easier indeed.

    However, before I can go back and test that, I need to find out what the heck errors like the following mean. They are coming from things that aren't in my code - I assume in some intermediary file(s) the compiler generates:
    Code:
    dave@davePC:~/juno-address-book$ gcc nvconvert.c nvconvert
    nvconvert: In function `type_Email':
    (.text+0x2fd): multiple definition of `type_Email'
    /tmp/cchG0agW.o:nvconvert.c:(.text+0x203): first defined here
    nvconvert: In function `type_Primary_Phone':
    (.text+0x579): multiple definition of `type_Primary_Phone'
    /tmp/cchG0agW.o:nvconvert.c:(.text+0x44f): first defined here
    nvconvert: In function `type_Deleted':
    (.text+0x705): multiple definition of `type_Deleted'
    /tmp/cchG0agW.o:nvconvert.c:(.text+0x5db): first defined here
    nvconvert: In function `type_Time':
    (.text+0x789): multiple definition of `type_Time'
    /tmp/cchG0agW.o:nvconvert.c:(.text+0x65f): first defined here
    nvconvert: In function `type_Address':
    (.text+0x681): multiple definition of `type_Address'
    /tmp/cchG0agW.o:nvconvert.c:(.text+0x557): first defined here
    nvconvert:(.bss+0x10): multiple definition of `wrk4'
    /tmp/cchG0agW.o:(.bss+0xc): first defined here
    nvconvert:(.bss+0x8): multiple definition of `wrk2'
    /tmp/cchG0agW.o:(.bss+0x4): first defined here
    nvconvert: In function `_fini':
    (.fini+0x0): multiple definition of `_fini'
    I don't understand what I did to make all of that come up. There is a LOT more listed after the above, but I suspect if I can find out what the heck caused the above and correct it that the rest will follow suit.

    As you can tell, I'm really lost now.

  6. #16
    Join Date
    Apr 2012
    Beans
    7,088

    Re: C, fprintf with commas

    Did you perhaps mean

    Code:
    gcc nconvert.c -o nconvert

  7. #17
    squakie is offline I Ubuntu, Therefore, I Am
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C, fprintf with commas

    That's what I *thought* I was doing, but you never know.....so, I went back, cleared the screen, did the gcc as shown and still get these odd errors. The source looks fine. The errors are coming from things not directly in my program. I can't even test until I can get rid of those.

    You know, it's been 20 years since I used C except for a few projects here and there. I've obviously forgotten more than I ever knew, and I sometimes get the feeling that what I *think* is right is something getting scrambled up with something from 20 years ago and it ends up a mess.

    I'm so sorry that I am so stupid on all of this. As I mentioned before, since I am starting over "green", if this isn't the correct place to post this please let me know.

    Again, thanks everyone!

  8. #18
    squakie is offline I Ubuntu, Therefore, I Am
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C, fprintf with commas

    stelldriver you were 100% correct on the gcc line. I *still* made that mistake and didn't catch it. So I went back and checked again, and indeed I didn't include the "-o ". It compiles now, and everything I've done so far is working, except that I need to do the truncation of the end-of-line character(s). I'm going to try that now.

    THANKS!!!

  9. #19
    squakie is offline I Ubuntu, Therefore, I Am
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C, fprintf with commas

    Okay, I think that's got is for now. I'm okay backing up 2 characters on the end of line because the input file is a Juno address book file, and Juno (in this case) is running on a Windows box, so when I copied the file to flash and brought it to my linux box to try to do this the file is still in dos format with the cr/lf's at the end of the line.

    I'm not sure how you code something to where you can say if the string contains 1 of these characters (the various end of line characters) then give me an index to it. To make the code more adaptable for others I should probably do that.

    I also don't remember squat about getting and using command line variables, so I've got to work on that as well.

    So, everything is "okay" to the best I can tell right now - I just need to do some more work!

    Thanks!

  10. #20
    squakie is offline I Ubuntu, Therefore, I Am
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: C, fprintf with commas

    BTW - does anyone know where I can find a definition of all the tags the online gmail expects in an impor? I know the first line of the file has to contain those names in a comma-seperated format - I just don't know what all of those field names are.

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •