Hi,
I have a problem creating one part of the program,
Lets say we have a line (from data file): 333 hou 23se 444 bi 4g
and the program should change to : house big
deleting first number if there is one ,and changing word parts to numbers, and deleting "just numbers" from line like 444...
code:

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include <ctype.h>
#define FILENAME "file.txt"

int main(void)
{
  char record[100],                    /* array to hold lines */ 
       *fld1, *line,*kk;                  /* pointers to lines */
  FILE *fin;                               /* pointer to input file */  
  int g,i;                                   /* some integers... */
  fin = fopen(FILENAME, "r");    /* open the file to read */
                                          
   
  while (fgets(record, sizeof(record), fin))
  {
    if (record[strlen(record) - 1] != '\n')
      record[strlen(record) - 1] = '\n';          //until new line
      line = strtok(record, "\n");                  //now we have a line to work with...
    
     i = 0;
     g =  strlen(line);

// this part of program should delete first number if there s one like : 33 dd to :dd   
  
  while( isdigit(line[i]))               
         {            
                line[i]= line[i+1]  ;   //somethings wrong... :(                  
                i=i+1;
         }
       

              // Ideas ?
       
                        
 
    printf("%s\n", line);
  }     
  fclose(fin);
  system("Pause");
  return 0;
}

lines 18 and 19. Why do you want to overwrite the last character read by '\n'? Don't you want to append it to the end of the string instead of overwriting the last character ? Actually I don't know why you want to do that anyway -- we normally just delete the '\n' if it exists.

line 20: useless line. record is already the full line that was read from the file. There is no reason to call strtok() for that.

>> ideas ?

Yes, here is one solution

int main()
{
    char line[] = "333 hou 23se 444 bi 4g";
    char buf[100] = {0};
    char *p1 = line;
    char *p2 = buf;
    while( *p1 )
    {
        // use temp pointer to locate
        // end of digits
        char* p3 = p1;
        while( isdigit(*p3) )
            p3++;
        // is there a space following the digits ?
        if( *p3 == ' ')
            // yes, then skip the space and move on to the next char
            p1 = p3+1;
        else
        {
            // no, then was the last char put into temp buffer
            // a space
            if( *(p2-1) == ' ')
                // yes, then overwrite that space with a new character
                p2--;
            p1 = p3;
        }
        // copy all characters up to the next digit
        while(*p1 && !isdigit(*p1) )
            *p2++ = *p1++;
    }

    return 0;
}

Hi,
Firstly,thanks for great idea!
Secondly, sorry that I didn't replied earlier...
Anyway I have one more question : if for egz.: we are reading multiple lines from text,
and we want to print them to the monitor, because of this code transformed lines are moved to buf and then printed, then moved to buf and then printed so we get
(input)
:333 hou 23se 444 bi 4g
333 hou 23se 444 bi 4g
333 hou 23se 444 bi 4g
(output):
house big
house bighouse big
house bighouse bighouse big
//-------------------------------------------
char buf[100] = {0};
char *p1 = line;
char *p2 = buf;

So how to free the memory of buf or exactly *p2?(that we could put a new line in it)
, free(buf); don't work... if *p2='\0'; then nothing again...
Well about lines (from above page 18,19), it was somehow connected with making a line with which we could work...
But probably it would be better if program would read everything into a line like
333 hou 23se 444 bi 4g\n333 hou 23se 444 bi 4g\n333 hou 23se 444 bi 4g\n
and then work with it?

You could turn Ancient Dragon's code into a function and then use it to process your input file line-by-line. It could look something like (the function is here AD())

void AD(char * record, char * record_out)
{
    char *p1 = record;
    char *p2 = record_out;

    // Rest of the original code here ...
}
// In your main(), the while loop
while (fgets(record, sizeof(record), fin))
{
    // A buffer local to the while() loop, of same size as the record array
    char record_out[sizeof(record)] = {0};

    // Process the line
    AD(record, record_out);

    // Display what we got
    puts(record_out);
}

Thanks a lot , everything works just fine :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.