help with c program to read each line from text file, split line , process and output
hello , here is what i have until now:
here is a sample line on my text file 1000 234 235 234 1000 9000 988 7877 122 234 233 need to assign variables on all numbers and output result in a similar way. any ideas? thank you in advance. #include <stdio.h> int main ( void ) { static const char filename[] = "testingcelebpoints.txt"; FILE *file = fopen ( filename, "r" ); if ( file != NULL ) { char line [ 128 ]; /* or other suitable maximum line size */ while ( fgets ( line, sizeof line, file ) != NULL ) //read a line { fputs ( line, stdout ); /* write the line */ } fclose ( file ); } else { perror ( filename ); /* why didn't the file open? */ } return 0; } |
for every line you get you could split it using strtok delimiting with a space...
|
Please post source code in [CODE] tags so we can all read it the way you wanted it to look.
Your one-line sample leaves some questions. If all lines in the file contain the same formatting with respect to the number of whitespace-delimited numeric fields, then fscanf() is your easiest route. Code:
assignments = fscanf( file, "%d %d %d %d %d %d %d %d %d %d %d", |
thanks guys
i ended up working out a different not so C like solution but i am still stuck.
look at this now: #include <string> #include <sstream> #include <fstream> #include <iostream> int main() { std::ifstream f("myfile.txt"); // Read all lines std::string line; while(std::getline(f, line)) { //assign variables to values std::istringstream tmp(line); std::string pic,tl1,tl2,br1,br2,le1,le2,re1,re2,n1,n2,m1,m2; tmp >> pic>>tl1>>tl2>>br1>>br2>>le1>>le2>>re1>>re2>>n1>>n2>>m1>>m2; //here i need to convert to int and work on the variables to output a new textfile with different data.// //out .. std::cout << whatever variable you want << "\n"; } } i tried using atoi but i can not make it work with the string file. thank you for your help, |
C++ streams will convert to int when reading to an int variable.
Code:
int pic,tl1,tl2,br1,br2,le1,le2,re1,re2,n1,n2,m1,m2; [code] your code here [/code] and don't use yellow text, it's unreadable. |
here is a line from my data file:
elton5.jpg 282 114 365 214 294 150 332 150 309 171 317 194 i used your suggestion, thank you . but my numbers do not make any sense?any ideas why? PHP Code:
|
Code:
printf("formatted line: %d %d %d \n",1,var1+var3,var2+var4); --- rod. |
aren't fputs, fgets and fscanf all C functions?
Digressing back to his second example (first c++), it should go a little like this: Code:
Quote:
|
why not do something along the lines of
Code:
whatever you do you should probably use a dynamic storage mechanism, unless you know that you will ALWAYS have the same number or less of elements per line. |
granted its mostly c++ psuedocode but you get the gist of whats going on i'm sure
|
Here's a example program that will parse as many lines of text you have in one or more files; it displays the tokens (individual "word") on the standard output although you could assign a token to a variable if that suits your needs. It's C -- not C++ -- but there you are. Hope it helps some.
Code:
#include <stdio.h> |
it is working now with your help but..
here is the code.
it works fine until the 15th line. after that the loop stops. i did not take a look at it yet. i will post the code when its debuged. [HTML]int main() { std::ifstream file; file.open("my file"); // Read all lines std::string line; while(std::getline(file, line)) { std::istringstream tmp(line); std::string pic; int var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11,var12; file >> pic >> var1 >> var2 >> var3 >> var4 >> var5 >> var6 >> var7 >> var8 >> var9 >> var10 >> var11 >> var12; printf("formatted line:%d %d %d \n",1,(var1+var3)/2,(var2+var4)/2); } }[/HTML] thank you all for your help. sorry for jumbing from c to c++. you are the best. |
here it is
here is the complete code:
[HTML]#include <string> #include <sstream> #include <fstream> #include <iostream> #include <cstdlib> int main() { std::ifstream f("testing-all-sunglasses.txt"); // Read all lines std::string line; while(std::getline(f, line)) { //assign values to variables std::istringstream tmp(line); std::string pic; float tl1,tl2,br1,br2,le1,le2,re1,re2,n1,n2,m1,m2; tmp >> pic>>tl1>>tl2>>br1>>br2>>le1>>le2>>re1>>re2>>n1>>n2>>m1>>m2; //out .. std::cout <<1<<" "<< (tl1+br1)/2 <<" "<<(tl2+br2)/2 <<" "<< "\n"; } }[/HTML] thank you all for your help. that was my first post on these forums so forgive me for my mistakes. i will try to give back the time you spend on my program to the forum . thanks |
All times are GMT -5. The time now is 03:30 PM. |