Thread: Help with my code, Please!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Unhappy Help with my code, Please!

    Hi all,
    I have a text file like this:
    abc c1 195
    abc c1 231
    RE c1 174
    RE c1 978
    HI c1 1125
    HI c1 1125
    ELA c1 269
    and I want to sum up the values from the third column for the same name in the first column, e.g., for abc, there are two values, 195 and 231, I want to add them up and generate a final txt file like:
    abc c1 xxx
    Re c1 xxx
    HI c1 xxx
    ELA c1 xxx
    I got some C code:
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(int argc, char *argv[])
    {
    	FILE *fin=fopen(argv[1],"r");
    	FILE *fout=fopen(argv[2],"w");
    		
    	char t[15],t_d[15];
    	char c[16],c_d[16];
    	int tag,tag_d;
    	int a;
    	
    	while (fscanf(fin, "%s %s %d", t,c,&tag)==3)
    	{
    		a++;
    		int b=0;	
    		while (fscanf(fin, "%s %s %d", t_d,c_d,&tag_d)==3)
    		{	
    			b++;
    			if(a==b)
    				continue;
    			else if(t==t_d)
    				tag += tag_d;
    		}
    		fprintf(fout, "%s %s %d\n", t,c,tag);		
    	}
    	fclose(fin);
    	fclose(fout);
    	return 0;
    }
    This code has no error, even in the Gdb debugging. But it even does not go to the if(a==b), and the first loop only loop once. I have no idea.
    I am new in C coding. Please help with any comment.

    Thanks a lot in advance.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    int a;
        
        while (fscanf(fin, "%s %s %d", t,c,&tag)==3)
        {
            a++;
    What is the value of a when you first increment it? Answer: could be anything.

    Code:
    else if(t==t_d)
    Are you aware that you cannot compare C-strings using ==, but that you must use strcmp?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Unhappy Thanks, it still does not work!

    Quote Originally Posted by rags_to_riches View Post
    Code:
    int a;
        
        while (fscanf(fin, "%s %s %d", t,c,&tag)==3)
        {
            a++;
    What is the value of a when you first increment it? Answer: could be anything.

    Code:
    else if(t==t_d)
    Are you aware that you cannot compare C-strings using ==, but that you must use strcmp?
    Thanks, I changed code accordingly, but still doesn;t work!
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(int argc, char *argv[])
    {
    	FILE *fin=fopen(argv[1],"r");
    	FILE *fout=fopen(argv[2],"w");
    		
    	char t[15],t_d[15];
    	char c[16],c_d[16];
    	int tag,tag_d;
    	int a=0;
    	
    	while (fscanf(fin, "%s %s %d", t,c,&tag)==3)
    	{
    		a++;
    		int b=0;	
    		while (fscanf(fin, "%s %s %d", t_d,c_d,&tag_d)==3)
    		{	
    			b++;
    			if(a==b)
    				continue;
    			else if(strcmp(tr_name,tr_name_d)==0)
    				tag += tag_d;
    		}
    		fprintf(fout, "%s %s %d\n", t,c,tag);		
    	}
    	fclose(fin);
    	fclose(fout);
    	return 0;
    }

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    of course it doesn't, your use of a and b is completely insane
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    are you sure "fscanf" can read " " (space I mean) from text file??

    as I remember, you can use "strtok"..
    get line per line and save it at temp variable..
    and then,
    temp = "abc c1 195" right now.. (for the first looping)
    you must use strtok to seperate one by one and save to each variable..

    and one more.. about "only loop for once".. while you are reading txt..
    use "EOF" at looping..
    while(!EOF(*pointer)){
    bla bla bla

    }
    Last edited by Kinshara; 10-06-2010 at 10:35 AM.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    What do you use?

    Quote Originally Posted by ಠ_ಠ View Post
    of course it doesn't, your use of a and b is completely insane
    What do you use instead of a or b?

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Kinshara View Post
    are you sure "fscanf" can read " " (space I mean) from text file??

    as I remember, you can use "strtok"..
    get line per line and save it at temp variable..
    and then,
    temp = "abc c1 195" right now.. (for the first looping)
    you must use strtok to seperate one by one and save to each variable..

    and one more.. about "only loop for once".. while you are reading txt..
    use "EOF" at looping..
    Ehh, I think you also need to read the FAQ.

    Don't use EOF to control a loop reading from a stream until EOF.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Do you really need to do it in C?(is it assignment?)
    You could just simply use awk or perl...

    Is your abc abc always going to appear consetively? If not you need to store them.
    Last edited by Bayint Naung; 10-06-2010 at 11:13 AM.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Question Does anyone have a working solution for this code???

    Guys, can any experienced C programmer work out a real code for my problem?
    Thanks a lot!!!

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Exclamation I need to learn C!

    Quote Originally Posted by Bayint Naung View Post
    Do you really need to do it in C?(is it assignment?)
    You could just simply use awk or perl...

    Is your abc abc always going to appear consetively? If not, you need to store them, probably use hash table.

    Thanks for your question.
    The text file actually is really lengthy which has multi million lines. The first field of each line could appear again and again, that's why I 'd like to sum it up. My idea is to run dual loops to check whether any line in the first loop can match the inner loop lines. After the two loops I can sum them up. I did it already in Python using the same algorithm, I just need it in C to speed it up.
    Thanks.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you have already done this in python, I suppose you already have the logic.
    That shouldn't be hard to translate to C. Isn't it?

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You know, where disk I/O is involved, I don't think there's going to be much of a speed difference between C and Python.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bayint Naung
    That shouldn't be hard to translate to C. Isn't it?
    It is not that easy because a naive implementation might result in worse performance than the Python version.

    billMD, must you use C for this? I might suggest C++ due to certain facilities available in the C++ standard library, otherwise you will have to either implement them yourself in C, or use a non-standard C library.

    In particular, it seems that you want to map strings to integers such that multiple instances of the string in the input will have their corresponding integer values added together. The thing is, you apparently have a pretty large amount of input, so searching an array may be not be a good idea unless it is sorted, but keeping the array sorted would make insertions expensive. Consequently, you might use a balanced binary tree or hash table to implement the mapping.
    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

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did someone mention "speed"?

    BillMD, Your Python program can be sped up by 10X, imo. I need to tinker with it, just a bit to test it and optimize it. We have an excellent record of beating the pants off any Python program's run-time.

    What I'd like to get is a text file that you run in 30 seconds or so, in Python. If you will upload it to a file transfer web site like Swoopshare (free), and post up the URL to the file, (or attach it to your posted reply here, using the "manage attachments" button - there is a limit of 488 KB). I can get some tests underway.

    Is this on a PC with a Windows or a Linux operating system? What was the cpu of the Python system the run-time was tested on?

    There may be rumors that I'm a bit competitive with these kinds of things - don't believe a word of it. Not a word. Bill, not a damn word!! Send the file, Bill!

    P.S. Would this same data file be processed just once, or is it a job, where the same data is having text lines added to it, and the same file then being re-processed regularly?
    Last edited by Adak; 10-06-2010 at 12:10 PM.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It took me a while to figure out what you're doing. I assume you don't expect a similar label somewhere down the list, so therefore we can just initialize the total once the label changes.
    Code:
    int main(int argc, char *argv[])
    {
    	FILE *fin = fopen(argv[1],"r");
    	FILE *fout = fopen(argv[2],"w");
    	char t[15], t_d[15], c_d[16];
    	int tag, tag_d;
    
    	if (NULL == fin || NULL == fout) {
    		printf("Failure to open in or out files.\n");
    		return 0;
    		}
    		
    	t[0] = '\0'; /* initialize to empty string */
    
    	while (fscanf(fin, "%s %s %d", t_d, c_d, &tag_d)==3) {
    		if (strcmp(t, t_d)) {
    			if (t[0])
    				fprintf(fout, "%s %s %d\n", t, c_d, tag);
    			tag = 0;
    			}
    		tag += tag_d;
    		strcpy(t, t_d);
    		}
    	fprintf(fout, "%s %s %d\n", t, c_d, tag);
    
    	fclose(fin);
    	fclose(fout);
    
    return 0;
    }
    I realize that doing homework is against board policy, but the OP had most of it alright. Just needed a bit of clean-up and simplification.
    And a real compile & run test to verify everything is OK.
    Last edited by nonoob; 10-06-2010 at 12:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM