LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2005, 01:58 AM   #1
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143
Blog Entries: 8

Rep: Reputation: 15
how to read a file into an array


I have a very newbyish C question.
I am making a game and I am stuck.I need to read a world map file that is 1000 characters long and 30 characters tall into an array map[30][1000].

for(int x=0;x<=worldheight;x++)
{
//map[x]=read a string 1000 characters long;
}

i've tried fscanf ,fread,fgets ,but the file position seems to be off or something.Im a little new to c++ so c code is easier to understand for me.Please help me.
 
Old 09-23-2005, 02:45 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Is this a text file? If yes, every line ends in a newline character ('\n', ASCII 0x0A), that's why the file position is off.
 
Old 09-23-2005, 03:51 AM   #3
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143
Blog Entries: 8

Original Poster
Rep: Reputation: 15
Its a unicode file created with gedit.What application should I use for an ascii file?It seems to be off quite a bit.Here is more code showing some of my attempts .I am shortening my world to 100 characters until I figure this out.

for(int x=0;x<=worldheight;++x)
{
// for(int y=0;y<=worldwidth-1;y++)
// {
// fgets(world[x],worldwidth,WORLD);

fread(world[x],sizeof(char),worldwidth,WORLD);
// fseek(WORLD,x*worldwidth,SEEK_SET);
printf("world y =%d \n",strlen(world[x]));
printf("world[x] =%s \n",world[x]);
// }
}

If I know the length of the newline character maybe I can fseek past it ?or shorten my reads by that amount?
Any example code will be contibution to my eventually open source game
 
Old 09-23-2005, 07:11 AM   #4
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally posted by cyb0rg777
Its a unicode file created with gedit.
What UNICODE encoding is it? If it's UTF-8 and you use only ASCII characters (0 to 127), this shouldn't be a problem. Otherwise: save it as ASCII encoded (I don't know how to do that in gedit, look for something like "Encoding").

Then you can use fgets() for every line and copy the first 100 characters into the array (don't forget error checking: the file might be corrupt). If the file is not corrupt (ie. 100 characters per line, plus newline-character), then there's no need for fseek().
 
Old 09-23-2005, 08:12 AM   #5
cyb0rg777
Member
 
Registered: Dec 2004
Location: ~
Distribution: Debian
Posts: 143
Blog Entries: 8

Original Poster
Rep: Reputation: 15
OK I got it .My file is a UTF-8.Here is the code I used .Maybe it will help someone else.


for(int x=0;x<=worldheight-1;x++)
{
if(fgets(world[x],worldwidth+1,WORLD)==NULL)cout<<"error reading world \n";
cout<<"world length "<<strlen(world[x])<<"\n";
cout<<"world data "<<world[x]<<"\n";
}

With some error checking.Thanks a bunch for the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading from a txt file into a two dimension array in C kponenation Programming 3 11-26-2005 07:04 PM
[bash] Put words from file to array mispunt Programming 4 11-04-2004 10:53 AM
char array of size 10 can read morethan 10 chars!!!!! pippet Programming 13 07-12-2004 01:44 AM
how do I make a read-only file NOT read-only? robster Linux - General 1 02-17-2004 09:11 PM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration