organizing text II

This is a discussion on organizing text II within the C Programming forums, part of the General Programming Boards category; Guys, I'm newbie in c programming. I'm currently have a problem in editing a text file. I got an input ...

  1. #1
    Registered User yohannes's Avatar
    Join Date
    Jun 2010
    Posts
    11

    organizing text II

    Guys, I'm newbie in c programming. I'm currently have a problem in editing a text file. I got an input like this.
    Code:
    "                        ","            ","          ","                                                                                "
    "                        ","            ","          ","                                                                                "
    "PB-CTRL                 ","FC      1   ","FC      1 ","                                                                                "
    "I/L-ROUTE               ","FC      2   ","FC      2 ","                                                                                "
    "COMM_TO_LOGGER          ","FC     27   ","FC     27 ","                                                                                "
    "10-12B-CTRL             ","M       0.0 ","BOOL      ","                                                                                "
    "10-22B-CTRL             ","M       0.1 ","BOOL      ","                                                                                "
    "10-42B-CTRL             ","M       0.2 ","BOOL      ","                                                                                "
    and I need to make the output to be like this
    Code:
    401,10-12B-CTRL,,,,,,,,PLC-OUT,,#,
    402,10-22B-CTRL,,,,,,,,PLC-OUT,,#,
    403,10-32B-CTRL,,,,,,,,PLC-OUT,,#,
    All I need from the first file is strings from the sixth line, actually only the "10-12B-CTRL". meanwhile, I have to add numbers, starting from 401, to every line. I have to add other characters too. It's like a template. Well if it consists just 3 lines, it'll be easy, but it doesn't. I got thousands of it. Need some help please. I've tried to program. I have my program like this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX 256
    
    int InsBaris (FILE *filesrc, FILE *filedest){
    	char str[MAX];
    	
    	fgets (str, MAX, filesrc);
    	while (!strstr (str,"\"COMM_TO_LOGGER          \",\"FC     27   \",\"FC     27 \",\"                                                                                \""))
    	{
    	fgets (str, MAX, filesrc);
    	}
    	
    	do {fgets (str, MAX, filesrc);
    		fputs (str,filedest);}
    		while (!feof(filesrc)) ;
    	return 1;
    }
    	
    int main (){
    	int Jum;
    	FILE *pfsrc, *pfdest;
    	char namafilesrc[MAX] = "variable-bangil-VDU.sdf";
    	char namafiledest[MAX] = "hasil.txt";
    	
    	pfsrc = fopen (namafilesrc, "r");
    	
    	if (pfsrc == NULL) 
    	{printf ("kesalahan! File coba.txt tidak dapat dibuka");
    		exit (EXIT_FAILURE);
    	}
    	
    	pfdest = fopen (namafiledest, "w");
    	
    	if (InsBaris(pfsrc, pfdest)){
    		printf ("\n Proses copy file berhasil");
    		}
    		else {
    		
    		printf ("\n Proses copy file gagal");
    		}
    		fclose (pfsrc);
    		fclose (pfdest);
    		return 0;
    		}
    This code has successfully left this
    Code:
    "10-12B-CTRL             ","M       0.0 ","BOOL      ","                                                              "
    "10-22B-CTRL             ","M       0.1 ","BOOL      ","                                                              "
    "10-42B-CTRL             ","M       0.2 ","BOOL      ","                                                              "
    in output.

    What I want to ask:

    1. how can I take only 10-12B-CTRL from the first line? and 10-22B-CTRL and the next line? and 10-44B-CTRL in the third line? I want to use pointer to char, but, how?
    2. how to make additional string to the output as mentioned above.

    those just beginning. there are more functions I'm still struggling with. thanks before.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For picking out parts of a string from a file, it's usually best to put the text into one char array, one line at a time, and then do your testing and pick out anything from it, that you need.

    fgets() is a great way to do that:

    Code:
    while((fgets(charArrayName, sizeof(charArrayName), filePointer)) != NULL) {
      //rest of your code in here
    
    }
    Inside the while loop, it looks like you could use strstr() to ID any line of text with CTRL in it, and then copy the first several char's out of the charArray, when you find the right line.

    The rest of your data can be found with either strstr() or strchr(), or with a function using your own logic - whatever you prefer.

    You'll need to include string.h for the string functions. Once you get familiar with using them, you'll like what they can do to make your programming easier.

  3. #3
    Registered User yohannes's Avatar
    Join Date
    Jun 2010
    Posts
    11

    misunderstood

    hi Adak! thanks for the reply. but, i think there's some misunderstanding here. I already got the lines with CTRL in it. just like what my output shows. but, I don't know how to extract the information within the line. i want to extract the numbers. but I don't know how. I tried to use Salem advice in another similar thread, but I just don't know where my fault is. my updated code just like this (do-while loop in function InsBaris )
    Code:
    do {fgets (str, MAX, filesrc);
    		size_t find_len1 = strlen("\"");
    		size_t find_len2 = strlen(" ");
    		if ((strncmp( str, "\"",  find_len1) == 0 ) || (strncmp( str, " ", find_len2)== 0)){
            char *p = str + find_len1;
    		strncpy (buffer, p, 22);
    		fputs (buffer,filedest);}
    		}
    		while (!feof(filesrc)) ;
    but, the result is not like what i want. it results
    Code:
    10-12B-CTRL           10-22B-CTRL           10-42B-CTRL
    what can I do about it? I want to select only strings between " and [space] from the line and put it in the buffer.
    I guess it's because the size of array. but, I can't change it, because somewhere in the input text file, there will be a string "23B2/13B3-SWRLSPB-CTRL" which consists of 22 characters.
    Code:
    "23B2/13B3-SWRLSPB-CTRL  ","M     247.2 ","BOOL      ","                                                                                "
    "23B1/13B2-SWRLSPB-CTRL  ","M     247.3 ","BOOL      ","                                                                                "
    "13B1-SWRLSPB-CTRL       ","M     247.4 ","BOOL      ","                                                                                "
    "R33-SWRLSPB-CTRL        ","M     247.5 ","BOOL      ","                                                                                "
    "R31A2-SWRLSPB-CTRL      ","M     247.6 ","BOOL      ","                                                                                "
    is there any solution for it?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by yohannes View Post
    Guys, I'm newbie in c programming. I'm currently have a problem in editing a text file. I got an input like this.
    Code:
    "                        ","            ","          ","                                                                                "
    "                        ","            ","          ","                                                                                "
    "PB-CTRL                 ","FC      1   ","FC      1 ","                                                                                "
    "I/L-ROUTE               ","FC      2   ","FC      2 ","                                                                                "
    "COMM_TO_LOGGER          ","FC     27   ","FC     27 ","                                                                                "
    "10-12B-CTRL             ","M       0.0 ","BOOL      ","                                                                                "
    "10-22B-CTRL             ","M       0.1 ","BOOL      ","                                                                                "
    "10-42B-CTRL             ","M       0.2 ","BOOL      ","                                                                                "
    and I need to make the output to be like this
    Code:
    401,10-12B-CTRL,,,,,,,,PLC-OUT,,#,
    402,10-22B-CTRL,,,,,,,,PLC-OUT,,#,
    403,10-32B-CTRL,,,,,,,,PLC-OUT,,#,
    All I need from the first file is strings from the sixth line, actually only the "10-12B-CTRL". meanwhile, I have to add numbers, starting from 401, to every line. I have to add other characters too. It's like a template. Well if it consists just 3 lines, it'll be easy, but it doesn't. I got thousands of it. Need some help please. I've tried to program. I have my program like this.

    What I want to ask:

    1. how can I take only 10-12B-CTRL from the first line? and 10-22B-CTRL and the next line? and 10-44B-CTRL in the third line? I want to use pointer to char, but, how?
    2. how to make additional string to the output as mentioned above.

    those just beginning. there are more functions I'm still struggling with. thanks before.
    The output is very different from the input, so it's not just a matter of "organizing text" I think. If you only needed the first part of the string my skepticism questions why the input itself has so much more stuff to it.

    Anyway this is just a bit of string parsing. I could solve it like so:

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main (void)
    {
        const char *canned[ 3 ] =
        {
            "\"10-12B-CTRL             \",\"M       0.0 \",\"BOOL      \",\"                                                              \""
            ,"\"10-22B-CTRL             \",\"M       0.1 \",\"BOOL      \",\"                                                              \""
            ,"\"10-42B-CTRL             \",\"M       0.2 \",\"BOOL      \",\"                                                              \""
        };
    
        const char *find_me[ 3 ] =
        {
            "10-12B-CTRL"
            ,"10-22B-CTRL"
            ,"10-42B-CTRL"
        };
    
        size_t i;
    
        size_t skip;
        const char *found;
    
        for (i = 0; i < 3; i++) { // scan canned example
            found = strstr (canned[i] , find_me[i]);
            if (found != NULL) {
                skip = strcspn (canned[i] , find_me[i]); // pinpoint location from start of line
                found = canned[i] + skip;
                printf ("%.*s\n" , (int)strlen (find_me[i]) , found); // if you can print it you can copy it
            }
        }
    
        return 0;
    }
    There are reasons I don't do it this way all the time. It's very inefficent if you are going to be using all the input anyway, and there is no concept of proper formatting. But it is also simple. All the work is done by the three functions strcspn, strstr, and strlen. All you need to know is what to look for and where to look (a string of search text).

    As for making it part of the output, you can use something like fprintf after you have all the outputs parts. The trickiest thing may be printing a specific number of commas, but that's just

    Code:
    fprintf (file , "%.*s" , numofcommasdesired , lotsofcommas);
    Last edited by whiteflags; 06-22-2010 at 05:13 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not a misunderstanding, really. strstr() will give you the line with CTRL in it. You don't want the first double quotation char in that line, but you want everything after that, until the next double quotation char.

    Code:
    A while loop will do that for you very nicely. 
    int i = 1;                   //you don't want the " at array[0]
    while(array[i] != '"') {
      ++i;
      fprintf your 401 control number, and each char to a line in an output file,
      add your 8 comma's (or whatever your # of fields per line require
    }
    
    "10-12B-CTRL             ","M       0.0 ","BOOL      ","                                                                                "
    The blue colored char's are saved. The data goes from the file, to your char buffer for fgets(), and THEN into your final file (or whatever). Trying to save straight from the file, into the buffer, with only the data you want, will be a headache for sure, don't do that.

    If you need to make these fixed length fields, use strlen() and add the needed spaces.

    I've done a lot of this kind of programming and it just takes some fiddling to get it right. Take it step by step, and remember, you have the whole line in your buffer - and with that, you can scan, and get, whatever you need. You can use string functions and also inspect it char by char, when needed.

  6. #6
    Registered User yohannes's Avatar
    Join Date
    Jun 2010
    Posts
    11

    complete problem

    Hi Adak and Whiteflags! thanks for the advices. but, the problem is more complicated than you thought. okay, i'll share the real problem.
    the M character and numbers next to it have meaning. It's the control for the numbers in output file
    M 0.0 => 401
    M 0.1 => 402
    M 1.1 => 409
    M 250.0 => 2401
    M 700.0 => 6001 ...
    M X.Y => 401 + 8*X + Y
    to sum up, I list several lines that represent my problems
    INPUT
    Code:
    "10-12B-CTRL             ","M       0.0 ","BOOL      ","                                                                                "
    "10-22B-CTRL             ","M       0.1 ","BOOL      ","                                                                                "
    "10-42B-CTRL             ","M       0.2 ","BOOL      ","                                                                                "
    "10-32B-CTRL             ","M       0.3 ","BOOL      ","                                                                                "
    "14-22A-CTRL             ","M       0.7 ","BOOL      ","                                                                                "
    "14-42A-CTRL             ","M       1.0 ","BOOL      ","                                                                                "
    "14-32A-CTRL             ","M       1.1 ","BOOL      ","                                                                                "
    "M10-EKR-DI              ","M     700.0 ","BOOL      ","                                                                                "
    "M11-EKR-DI              ","M     700.1 ","BOOL      ","                                                                                "
    "M12-EKR-DI              ","M     700.2 ","BOOL      ","                                                                                "
    DESIRED OUTPUT
    Code:
    401,10-12B-CTRL,,,,,,,,PLC-OUT,,#,  	//M 0.0 is 401
    402,10-22B-CTRL,,,,,,,,PLC-OUT,,#,
    403,10-42B-CTRL,,,,,,,,PLC-OUT,,#,
    404,14-22A-CTRL,,,,,,,,PLC-OUT,,#,
    405,,,,,,,,,PLC-OUT,,#,                                                   //because there is no M 0.4
    406,,,,,,,,,PLC-OUT,,#,                                                   //because there is no M 0.5
    407,,,,,,,,,PLC-OUT,,#,                                                   //because there is no M 0.6
    408,14-42A-CTRL,,,,,,,,PLC-OUT,,#,
    409,14-32A-CTRL,,,,,,,,PLC-OUT,,#,
    410,,,,,,,,,PLC-OUT,,#,                                                   //because there is no M 1.2
    411,,,,,,,,,PLC-OUT,,#,                                                   //because there is no M 1.3
    ...                                                                       //the template is continued
    6000,,,,,,,,,PLC-OUT,,#,                                                  //because there is no M 699.7
    6001,M10-EKR-DI,M10-EKR-DI turned on,M10-EKR-DI turned off,,,,,,,,#,      // M10-EKR-DI is used again in the next comma, PLC-OUT is no longer printed.
    6002,M11-EKR-DI,M11-EKR-DI turned on,M11-EKR-DI turned off,,,,,,,,#,      
    6002,M12-EKR-DI,M11-EKR-DI turned on,M12-EKR-DI turned off,,,,,,,,#,
    That's all folks!
    I have to check the numbers next to M,
    take the symbols in every input line,
    concat it with turned on and off,
    and put it in order one by one, separated with commas.
    what confuse me is the missing sequence numbers. I don't know how to make the template is continued printed when the M numbers is missing. I'm thinking about making a temporary file that contents
    Code:
    401,10-12B-CTRL,,,,,,,,PLC-OUT,,#,  	
    402,10-22B-CTRL,,,,,,,,PLC-OUT,,#,
    403,10-42B-CTRL,,,,,,,,PLC-OUT,,#,
    404,14-22A-CTRL,,,,,,,,PLC-OUT,,#,
    408,14-42A-CTRL,,,,,,,,PLC-OUT,,#,
    409,14-32A-CTRL,,,,,,,,PLC-OUT,,#,
    6001,M10-EKR-DI,M10-EKR-DI turned on,M10-EKR-DI turned off,,,,,,,,#,     
    6002,M11-EKR-DI,M11-EKR-DI turned on,M11-EKR-DI turned off,,,,,,,,#,      
    6002,M12-EKR-DI,M11-EKR-DI turned on,M12-EKR-DI turned off,,,,,,,,#,
    and then check every line once again and if the next sequence number is not prevnum+1, then print the template. how about it?
    Last edited by yohannes; 06-23-2010 at 01:59 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I suggest:

    Use fgets to put the line into your buffer. Working from the buffer one line at a time, is easiest, for now.

    Have a checkM function to handle M. Use a static variable perhaps, to check if the next line has the next M in the sequence, or not.

    Each line looks like a record with fixed length fields in it. C handles those with structs and struct members, nicely.

    Use separate blocks of code or functions, for each small task you need to do. Don't try and do a bunch of steps, all in the same block of code, or function. Simple step by step "meter by meter, life is sweeter".

    It may look VERY complicated, but it's not. Process some records with just a paper and pencil and watch what you're doing, while accomplishing that. In truth, anyone could do this, after some training, and soon would be bored to tears.

    The reason this is difficult is not because of the complexity of the problem. The reason is that you don't have a clear understanding of the STEPS you need to take, to process each record, quite yet. You can't solve the problem with a program, until you know those steps. One way to learn them, is to do the problem by hand, until you do have that understanding.

    It reminds me of when I was a kid in school. In those days (dinosaurs ruled the earth, IIRC), we just learned "Arithmetic", in elementary school, with no concepts of "math" theory. Then, somewhere between 8th and 10th grade, you were put into "Algebra", and it was SO dark and muddled at first. After awhile, the understanding of the steps being used arrived, and *BING* the light went on.

    That's what you need to do, nail down those steps. I'll be surprised if there are more than a dozen steps, to process each record. Concentrate on the steps, and put off the minute details of each step, for now. After you have the steps you need, and the overall design working, then handle the details you put off, earlier. That's top down design in a nutshell.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    142

    organizing text II (for PLCs)

    It appears to me that you are trying to transfer (or translate) the I/O and memory descriptions for a project from one Programmable Logic Controller (PLC) to another one with a different architecture. Or possibly from one set of programming and documentation software to another for the same PLC. Your input and output formats are clearly two different comma separated value (CSV) formats.

    If you are doing this professionally (also known as your being paid), I would seriously consider doing this using a spreadsheet program such as Microsoft Excel or equivalent. Most of these programs can directly read and write CSV formatted files. They would provide more flexibility in handling all the special cases that I suspect you are going to deal with. If you are costing someone money, you need to do this the fastest way possible.

    If you still have some strong reason to do this in the C programming language and you can use an existing library, I would look for some open source library that works with (reads and/or writes) CSV formated files. Make sure that you (and your organization, if applicable) can live with whatever license agreement is associated with the CSV library.

    If your are going to do this in any programming language, pay close attention to the advice that Adak gave you. You may have a several different sets specialized manipulations that you need to do probably based on the type of input, output, or memory. You will have to define these carefully if you are doing this with any programming language.

  9. #9
    Registered User yohannes's Avatar
    Join Date
    Jun 2010
    Posts
    11

    thanks. I'm working on it.

    Hi adak, and pheininger (not so easy to type, :P)! thanks for the advice. yeah, i'm sorry for being so dumb. this is my task in my office. I'm having an internship in a electronics industry in Bandung, Indonesia. but, they don't pay me for doing this . It's a mandatory subject in my third year in college: Industrial Practice. well, that's the truth. I'm not a programmer though. I'm an electrical engineering student. but, I took sub-major computer engineering. Computer engineering and software engineering is two different major I think. I learned C in college, yes. but there, I learned to view C in hardware's point of view. not software's. I use C a lot to program microcontrollers. and I learned a lot about ASM and VHDL, too. maybe, that's what make this programming task is so difficult for me. It's the first time I'm working with files! In programming tasks, I used to work with stdin and stdout, not files.


    well, so far I understand what I need to do. I think I need to use struct, don't I? If I made a struct Line, I need to scan one line, put the symbols in Line.symbol and scan again the line and put number next to M in Line.number.

    I'll tell you guys when I made a progress. Thanks again for the responds.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're on the right track. Each line of the text appears to be a record, and each record has several fields in it:

    Say for example, you had a record for students, what would the fields be?

    Code:
    struct student {
      unsigned long ID_number;
      char firstName[30];
      char lastName[40];
      int age;
      char sex;
      char major[30];
    }student;
    The above code makes a struct, which will be our field, and several struct members. Each struct member will be a field in the record.

    Now it's really easy to work with a students data, because it's all "glued" together, and each member can be accessed directly using the dot operator, or indirectly, using a pointer to struct operator: ->

    Once you identify each field for a record, then you need to identify the processing you need your program to do, to make each field contains the right info. Take each one, one at a time. Don't hesitate to use a separate function for each step, if it seems clearer and more intuitive.

    Don't even worry about the order of the struct members! You can take in your struct members in any order you want (although first to last is clearer), and you can output your struct members in any order you need. If I wanted to output the ID_number as the last member, instead of the first, that's no problem at all.

    I thought you were "messing" with us, but I see where you're coming from, now.

    If you search the forum for "database", you'll see several instances of flat file simple databases like the above. They show how to work with structs and struct members: input, output, sorting, editing, etc. You may not need them, but they are there, if you want to see some code examples.

    Hint: give your struct (name, member name, and array), descriptive but short, variable names. Long names in them, will make your lines of code, *very* long, and that doesn't display well.

  11. #11
    Registered User yohannes's Avatar
    Join Date
    Jun 2010
    Posts
    11

    progress

    thanks for the attention. my progress is this. I've made the output like this.
    Code:
    401,10-12B-CTRL           ,,,,,,,,PLC-OUT,,#,  	
    402,10-22B-CTRL           ,,,,,,,,PLC-OUT,,#,
    403,10-42B-CTRL           ,,,,,,,,PLC-OUT,,#,
    404,14-22A-CTRL           ,,,,,,,,PLC-OUT,,#, //want to print 405,,,,,,,,,PLC-OUT,,#, until 407,,,,,,,,,PLC-OUT,,#, but still can't... How???
    408,14-42A-CTRL           ,,,,,,,,PLC-OUT,,#,
    409,14-32A-CTRL           ,,,,,,,,PLC-OUT,,#,
    6001,M10-EKR-DI            ,,,,,,,,PLC-OUT,,#,  
    6002,M11-EKR-DI            ,,,,,,,,PLC-OUT,,#,      
    6002,M12-EKR-DI            ,,,,,,,,PLC-OUT,,#,
    and this is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include "itoa.c"
    
    #define MAX 256
    
    typedef struct {
         char symbol[MAX];
         char X[MAX];
    	 char Y[MAX];
        } Line;
    		
    int InsBaris (FILE *filesrc, FILE *filedest){
    	char str[MAX];
    	Line L;
    	
    	fgets (str, MAX, filesrc);
    	while (!strstr (str,"\"COMM_TO_LOGGER          \",\"FC     27   \",\"FC     27 \",\"                                                                                \""))
    	{
    	fgets (str, MAX, filesrc);
    	}
    	
    	while (!feof(filesrc))
    	{	char finalLine[MAX];
    		int x,y,z;
    		char num[6];
    		const char coma8[] = ",,,,,,,,";
    		const char plc[] = "PLC-OUT";
    		const char comaEnd[] = ",,#,";
    		
    		fgets (str, MAX, filesrc);
    		
    		size_t find_len1 = strlen("\"");
    		char *p1 = str + find_len1;
    		
    		strncpy (L.symbol, p1, 22); //insert symbol. still can't make it dynamic. How to make it copy strings and exit when it meets [space]??
    		L.symbol[22] = '\0';
    		strncpy(L.X, p1 + 31, 5); //if M 1020.1, X = 1020, Y = 1
    		L.X[5] = '\0';
    		strncpy(L.Y, p1 + 37, 1);
    		L.Y[1] = '\0';
    			
    		x = atoi (L.X);   
    		y = atoi (L.Y);
    		z = 401 + 8*x + y;
    		itoa (z, num, 10); //funct that converts from integer to char successfully
    		
    		strcpy (finalLine, num);
    		strcat(finalLine, ",");
    		strcat(finalLine, L.symbol);
    		strcat(finalLine, coma8);
    		strcat(finalLine, plc);
    		strcat(finalLine, comaEnd);
    			
    		fputs (finalLine,filedest);
    		fputs ("\n",filedest);
    		}
    
    	return 1;
    }
    my next questions are:
    1. (shown in my code) how to copy the symbol dynamically. if the symbol before the [space] consists of 11 characters, it copies 11. if it's 22, it copies 22, and so on. so the output is not like above, but like desired output.
    2. (shown in my output)I want to print template just like shown in my desired output. but still can't. can anybody help me? If I use a temporary file that contents just like what I got right now, but then I make function that scan the lines in the temp file to check whether the number of a line is more-one compared to previous line, do you think it'll efficient? or do you have any other way to get my desired output?

    thanks a lot.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll probably want to replace using feof, with:
    Code:
    while((fgets(stringArrayName, sizeof(stringArrayName), filePointer)) != NULL) {
    feof() doesn't work like you think, and has a big tendency to output the last line, twice.

    I'd have liked:

    1) To see more members in your struct - that will help nail down the variables in your loop, and keep them organized better.

    2) You're using C99 code, which very few of us can run. If you want specific code help, I'd change it back to having all your variables declared at the very top of any function they're declared in.

    A bit of comments, in your code, can help keep you on track with your algorithm. Not the obvious stuff - but the "big picture" and obscure stuff that makes you go "huh"? when you first look at it. NOT SIMPLE PRINT STATEMENTS, increments/decrements, and such!!

    Very helpful if it's concise, and descriptive.

    The obvious approach to printing 405 through 407, is to use a for loop:

    Code:
    if(this number > last row's number +1) {
      for(each missing number) {
        print the missing row of data
      }
    }
    I do this kind of thing a lot, and have a shorthand. A variableName that has to be remembered becomes "ovariableName" (old variable).

    Yur in luk, I habben't patented thet idear, yett!!

  13. #13
    Registered User yohannes's Avatar
    Join Date
    Jun 2010
    Posts
    11

    still confused

    thanks 4 da reply, I'm sorry for being so dumb. but I'm still confused about finding number of character that fit into L.symbol. so that, when I print L.symbol, I won't find " " (spaces).

    I tried to make function like this
    Code:
    int CheckSym (char a[])
    {	int counter=0;
    	int i=0;
    	char *temp;
    	*temp = a[i];
    	
    	while (strcmp (temp, " ") != 0){
    		counter ++;
    		*temp = a[i++];
    		}
    		return counter;
    }
    to find the length of symbol (i.e. 10-12B-CTRL (counter = 11), 1234-22B-CTRL (counter = 13), etc)
    and then use the return value for
    Code:
    void insSymbol(char *p, int counter){
    	strncpy (L.symbol, p, counter); 
    	L.symbol[counter] = '\0';
    }
    So far I assigned 22 for the counter variable because the longest symbol consists of 22 chars.

    and about the struct, I have made it like this
    Code:
    struct Line{
         char symbol[MAX];
         char X[MAX];
    	 char Y[MAX];
    	 int lastNum;
    	 char coma8[MAX] ;
    	 char plc[MAX] ;
    	 char comaEnd[MAX] ;
        } L = {"","","",400, ",,,,,,,,", "PLC-OUT", ",,#,"};
    and make it cleaner. thanks.

    about the missing rows, I use this
    Code:
      ....
    		x = atoi (L.X);   
    		y = atoi (L.Y);
    		z = 401 + 8*x + y;
    		if (z > L.lastNum + 1){
    			for (;z>=L.lastNum + 1;z--){
    			itoa (z, num, 10);
    			
    			strcpy (finalLine, num);
    			strcat(finalLine, ",");
    			strcat(finalLine, "");
    			strcat(finalLine, L.coma8);
    			strcat(finalLine, L.plc);
    			strcat(finalLine, L.comaEnd);
    			
    			fputs (finalLine,filedest);
    			fputs ("\n",filedest);
    			}
    		}
    		else {
    			L.lastNum = z;
    			itoa (z, num, 10); 
    			
    			strcpy (finalLine, num);
    			strcat(finalLine, ",");
    			strcat(finalLine, L.symbol);
    			strcat(finalLine, L.coma8);
    			strcat(finalLine, L.plc);
    			strcat(finalLine, L.comaEnd);
    				
    			fputs (finalLine,filedest);
    			fputs ("\n",filedest);
    			 }
    but, I got this..
    Code:
    401,10-12B-CTRL           ,,,,,,,,PLC-OUT,,#,
    402,10-22B-CTRL           ,,,,,,,,PLC-OUT,,#,
    403,10-42B-CTRL           ,,,,,,,,PLC-OUT,,#,
    404,10-32B-CTRL           ,,,,,,,,PLC-OUT,,#,
    408,,,,,,,,,PLC-OUT,,#,
    409,14-42A-CTRL           ,,,,,,,,PLC-OUT,,#,
    410,14-32A-CTRL           ,,,,,,,,PLC-OUT,,#,
    6001,,,,,,,,,PLC-OUT,,#,
    6002,M11-EKR-DI            ,,,,,,,,PLC-OUT,,#,
    6003,M12-EKR-DI            ,,,,,,,,PLC-OUT,,#,
    what's wrong??

    and about c99. I dunno which is the different. sorry. newbie..
    thx for the reply..

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    		x = atoi (L.X);   
    		y = atoi (L.Y);
    		z = 401 + 8*x + y;
    		if (z > L.lastNum + 1){     //is z too big?
    			for (;z>=L.lastNum + 1;z--){
    			itoa (z, num, 10);   //you're using z anyway!! 
    			//should be using L.lastNum +1
    			strcpy (finalLine, num);
    			strcat(finalLine, ",");
    			strcat(finalLine, "");
    			strcat(finalLine, L.coma8);
    			strcat(finalLine, L.plc);
    			strcat(finalLine, L.comaEnd);
    			
    			fputs (finalLine,filedest);
    			fputs ("\n",filedest);
    			}
    		}
    With the counter length, just count them:

    1) With strlen() if they are an actual string ending with an end of string char: '\0'
    2) by simple counting through from one set of " to the other ", in your char array, if they are char's that are not terminated (and thus not elevated to string status in C).

    There are many small steps to make, to do this, and it's tedious to work through, but make a list of every step your program needs to take, and tick them off in your code, one at a time.

  15. #15
    Registered User yohannes's Avatar
    Join Date
    Jun 2010
    Posts
    11

    I've fixed it... a little bit more

    this is my updated code
    Code:
         ....
                    y = atoi (L.Y);
    		z = 401 + 8*x + y;
    	
    		if (z > (L.lastNum + 1)){
    			for (L.lastNum = (L.lastNum + 1); L.lastNum < z ; L.lastNum++){
    			
    			itoa (L.lastNum, num, 10);
    			strcpy (finalLine, num);
    			strcat(finalLine, ",");
                            .....
    and this is the result
    Code:
    401,10-12B-CTRL,,,,,,,,PLC-OUT,,#,
    402,10-22B-CTRL,,,,,,,,PLC-OUT,,#,
    403,10-42B-CTRL,,,,,,,,PLC-OUT,,#,
    404,,,,,,,,,PLC-OUT,,#,
    405,,,,,,,,,PLC-OUT,,#,
    406,,,,,,,,,PLC-OUT,,#,
    408,14-22A-CTRL,,,,,,,,PLC-OUT,,#,
    409,14-42A-CTRL,,,,,,,,PLC-OUT,,#,
    410,14-32A-CTRL,,,,,,,,PLC-OUT,,#,
    411,14-52A-CTRL,,,,,,,,PLC-OUT,,#,
    where is 407? I've tried hard to find what's wrong, but still couldn't find it. 407 got symbols in it... thanks for the advice...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detect OS version
    By linucksrox in forum Windows Programming
    Replies: 4
    Last Post: 05-18-2010, 07:47 AM
  2. Replies: 8
    Last Post: 05-05-2010, 03:43 PM
  3. RichEditCtrl & unicode formatted text
    By ddonate in forum Windows Programming
    Replies: 0
    Last Post: 03-26-2010, 11:50 AM
  4. Scrolling The Text
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 07-14-2002, 05:33 PM
  5. Replies: 1
    Last Post: 07-13-2002, 06:45 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21