Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ : reading input from file

Started by penetrator Apr 23, 2003 at 4:04 PM 12 replies 650+ views
Original Post
penetrator
penetrator
Hi, how would i read data from a text file where values are separated by a comma ? For example: 1,2,3,4,5,6 ...
Fuzziqer_Software
Fuzziqer_Software
Well, I would write this:

#include <stdio.h>

FILE* fp = fopen(filename,"r+t");
char list[10][5];
bool item_finish;
int ch,x,y;
for (x = 0; x < 10; x++)
{
item_finish = true;
for (y = 0; y < 5; y++)
{
ch = fgetc(fp);
if (ch == '','') item_finish = true;
if (item_finish == false) list[x][y] = ch;
}
}

If you want to make a function out of it, you''ll need to put the variable ''list'' into shared memory or a global variable.

Also, if you want to convert a string with a number in it (i.e. "2537") to an int (i.e. 2537), call the function:
int atoi(char*)
penetrator
penetrator
Thx for helping: i tried Fuzziqer_Software example, but the array filled is empty, probably there is a bug somewhere in the routine ...

civguy
civguy
Here's yet an alternative

      
#include <iostream>

#include <fstream>

#include <algorithm>

#include <string>


int main() {
std::ifstream fin("pikkustedi.cpp");
while(fin) {
std::string text;
std::getline(fin, text, ',');
std::cout << text << '\n';
}
}


[edited by - civguy on April 23, 2003 7:09:43 PM]
penetrator
penetrator
ok, but how would i store data into an array ? I want for example smt like

listed[10][0]=1
listed[10][1]=2
listed[10][2]=3
listed[10][3]=4

Senses777
Senses777
why did civguy #include <algorithm>? Am I missing something here?

And penetrator, just use AP''s code slightly modified, it was the best (IMHO) C++ solution here.


  
#include <iostream>
#include <fstream>
using namespace std;

const int MAX_ARRARY_SIZE = 10;

void main()
{
int array[MAX_ARRAY_SIZE];
ifstream fin( "foo.txt" ); // fin > in


for(int i=0; fin && i<MAX_ARRAY_SIZE; i++)
{
fin >> array[i];
fin.ignore(INT_MAX, '','');
}
}


good luck! .sen
"I want to make a simple MMORPG first" - Fenryl
Senses777
Senses777
be more specific, I don''t understand your question. .sen
"I want to make a simple MMORPG first" - Fenryl
civguy
civguy
quote:
Original post by Senses777
why did civguy #include <algorithm>? Am I missing something here?
I remembered std::getline was there, but apparently it was in <string>.
quote:
I want to do that but read spaces also, but it just skips them what can I do?
Use the code I posted, it reads spaces too.
civguy
civguy
quote:
Original post by penetrator
ok, but how would i store data into an array ? I want for example smt like

listed[10][0]=1
listed[10][1]=2
listed[10][2]=3
listed[10][3]=4
You ought to read some introductory material if you don''t know how to store values into an array from the code I posted. Giving you a direct answer here wouldn''t get you far.
Senses777
Senses777
quote:
Original post by civguy
You ought to read some introductory material if you don''t know how to store values into an array from the code I posted. Giving you a direct answer here wouldn''t get you far.


good point. But I respond to everything that I read once the clock hits 3AM here. It''s almost like I''m spilling out dreams about programming, I can''t help but reply to any question that I know remotely anything about and leave messages that are way too long. Wait... I see where this is going...

Sleep well. .sen
"I want to make a simple MMORPG first" - Fenryl
penetrator
penetrator
i apologize for answering before reading better what you guys wrote. The code from Senses777 it''s ok. I''m currently trying to learn C++ and i found it quite complicated, and my english is not so good so often i don''t understand very well. Excuse me and thanks again to everybody !

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.