| | |
C working with string problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 22
Reputation:
Solved Threads: 0
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:
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:
c Syntax (Toggle Plain Text)
#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; }
Last edited by Ancient Dragon; Apr 23rd, 2008 at 11:03 pm. Reason: add code tags
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.
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.
Last edited by Ancient Dragon; Apr 23rd, 2008 at 11:16 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
>> ideas ?
Yes, here is one solution
Yes, here is one solution
C Syntax (Toggle Plain Text)
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; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 22
Reputation:
Solved Threads: 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?
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?
•
•
Join Date: Nov 2007
Posts: 979
Reputation:
Solved Threads: 209
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())
C Syntax (Toggle Plain Text)
void AD(char * record, char * record_out) { char *p1 = record; char *p2 = record_out; // Rest of the original code here ... }
C Syntax (Toggle Plain Text)
// 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); }
![]() |
Similar Threads
- Shopping Cart Not Working (ASP.NET)
- read to end of line problem (C)
- c-string (C++)
- need help with tricky string modyfication (C)
- Problem with querystring trimming (VB.NET)
- A string problem (C)
- AnsiString Template Data Return Problem Builder 6 (C++)
- msn messenger not working.. SSL (Windows NT / 2000 / XP)
- Array limit (C)
Other Threads in the C Forum
- Previous Thread: Hashing- Linklist Chaining
- Next Thread: Array of pointer to characters
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






