| | |
reading data from text fies in c
![]() |
you have a couple options:
1. use strtok() in a loop to extract the individual strings and copy the text into another string array.
2. use a pointer and strchr() to locate each colon then copy the text into another string up to the pointer that was returned by strchr().
1. use strtok() in a loop to extract the individual strings and copy the text into another string array.
2. use a pointer and strchr() to locate each colon then copy the text into another string up to the pointer that was returned by strchr().
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: Mar 2007
Posts: 4
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main () { char num[] ="1.00:2.00:3.00"; char *ch; char r[100]; printf ("split \"%s\":\n",num); ch = strtok (num,":"); while (ch != NULL) { i = 0; printf ("%s\n",ch); ch = &r[i]; ch = strtok (NULL, ":"); i ++; } return 0; }
--> here is my strtok code, but it doesn't work.
how can i copy the values of the pointer ch into a char array r? thanks.
Last edited by ~s.o.s~; Mar 22nd, 2007 at 2:22 pm. Reason: Added code tags, learn to use them.
Use [search]strcpy[/search] to copy the contents of the string pointed by ch to another string.
Last edited by ~s.o.s~; Mar 22nd, 2007 at 2:29 pm.
I don't accept change; I don't deserve to live.
Sacrifice is a painful, pure and beautiful thing.
Dammit, Jones, What the Hell Are Knoll Pointers?!
Sacrifice is a painful, pure and beautiful thing.
Dammit, Jones, What the Hell Are Knoll Pointers?!
•
•
Join Date: Mar 2006
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
hi
im asked to read data inputs from a text file in c.
my problem is i dont know to separate the values,
ex. may data contains these:
1.00:2.00:3.00
i would like to assign the values a = 1:00, b = 2.00, c =3.00.
how is it done?
thanks!
This is the code which u want...
/*******************************************/
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *fpInputFilePtr; float a[3]; char strTokan[] = {":"}; char *strLineInfo = (char*) calloc(256,1); char *ptrStrLineInfo = strLineInfo; int nTmpCnt=0; fpInputFilePtr = fopen("E:\\Test Projects\\Input.txt","r"); while(fgets(strLineInfo,256,fpInputFilePtr)) { strLineInfo[strlen(strLineInfo)-1]='\0'; while(strlen(strLineInfo)>1) { strtok(strLineInfo,strTokan); a[nTmpCnt]=atof(strLineInfo); strLineInfo += strlen(strLineInfo)+1; } strLineInfo = ptrStrLineInfo; memset(strLineInfo,'\0',sizeof(strLineInfo)); } /******** Do your work here*****/ if(fpInputFilePtr) { fclose(fpInputFilePtr); fpInputFilePtr=NULL; } if(strLineInfo) { free(strLineInfo); strLineInfo=NULL; } if(ptrStrLineInfo) { free(ptrStrLineInfo); ptrStrLineInfo=NULL; } return 0; }
If u have any queries feel free to mail to nrk_jegan@yahoo.com
Regards,
Jeganathan Krishnasamy.
Last edited by Ancient Dragon; Mar 23rd, 2007 at 8:40 am. Reason: add missing code tags
Sorry, Jeg, but your code doesn't work either. The lines (16-21) using strtok() are incorrect and will only find the first occurence of the colon and no others. Please test out your program to make sure it works before attempting to show someone else how to write a program.
Last edited by Ancient Dragon; Mar 23rd, 2007 at 8:45 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
•
•
Join Date: Mar 2006
Posts: 5
Reputation:
Solved Threads: 0
Definitely it'll work.Otherwise i won't put it here.I tested it and working fine.
plz once again check the code by running it or just debug it.I'm incrementing the character pointer.So each time i'm eliminating one ":".
This code is tested for 10 inputs from file.
plz once again check the code by running it or just debug it.I'm incrementing the character pointer.So each time i'm eliminating one ":".
This code is tested for 10 inputs from file.
Last edited by JeganathanK; Mar 26th, 2007 at 1:22 am.
here is how that loop should have been coded. Your program has a lot of unnecessary and just plain clumbsy code, such as the way it increments strLineInfo
>>memset(strLineInfo,'\0',sizeof(strLineInfo));
Since strLineInfo is a pointer the sizeof operator will always return the size of a pointer, which is 4 on most 32-bit compilers. So that memset line is pretty useless.
c Syntax (Toggle Plain Text)
while(fgets(strLineInfo,256,fpInputFilePtr)) { if( strLineInfo[strlen(strLineInfo)-1] == '\n') strLineInfo[strlen(strLineInfo)-1]='\0'; ptrStrLineInfo = strtok(strLineInfo,strToken); while(ptrStrLineInfo != NULL) { a[nTmpCnt]=atof(ptrStrLineInfo); ++nTmpCnt; ptrStrLineInfo = strtok(NULL, strTokan); } }
>>memset(strLineInfo,'\0',sizeof(strLineInfo));
Since strLineInfo is a pointer the sizeof operator will always return the size of a pointer, which is 4 on most 32-bit compilers. So that memset line is pretty useless.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
![]() |
Similar Threads
- C++ Reading from a text file (C++)
- Reading Data from Text (IT Professionals' Lounge)
- Help Please, how do i read from text file into array? (Visual Basic 4 / 5 / 6)
- How to select data frm text file based on a condition (C)
- Perl/CGI (Reading Data) Part II (Computer Science)
- Reading data into files (C++)
Other Threads in the C Forum
- Previous Thread: Dynamic array management
- Next Thread: drawing outside client area
Views: 1389 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays binary binarysearch c++ calculator calling changingto char code coke command conversion convert creafecopyofanytypeoffileinc data database decimal directory directorystructure dude dynamic error exec fflush(stdout) fgetc fgets file fork frequency function functions givemetehcodez grade graphics hangman homework i/o incrementoperators input insert int integer lazy line linked linkedlist linux list lists loop looping loopinsideloop. lowest malloc matrix measuring memory mysql no-code no-effort no-good open operator output path pointer pointers problem process program programming read recursion recursive recv reverse reversing scanf scripting send socketprograming spoonfeeding stack string strings strtok structures student syntax system telephone turbo-c turboc undefined unix user variable windows






