LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 02-19-2008, 10:47 PM   #1
cyborgt
LQ Newbie
 
Registered: Feb 2008
Posts: 5

Rep: Reputation: 0
plz need help on C++ about read int from a TXT to array.


Hi, I tried to do it by myself, didn't work out well, tried to find some hint online.... with no luck(i got more confused, in fact).

here is the detail, i havd a file .txt, there are 20 x 30 inputs, seprated with a space, randomly ranged from 0 to 255, i need to read them into an array in C++, and hope i can output them into another file.
here is what i got so far( worked, some way, which i have no idea what way).


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <strstream>

using namespace std;

int main()
{


ifstream fin("c:\\n.txt",ios::in);
int x;


int array[1000];
while (fin >> x)
{
for (int i=0; i<=10; i++)
array[i] = x;
cout << array[i] << " ";
}


return 0;
}
 
Old 02-20-2008, 12:44 AM   #2
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
You're almost there
You need to :
1. use the code tags
2.
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <strstream>

using namespace std;

int main()
{


ifstream fin("c:\\n.txt",ios::in);
ofstream fout("c:\\m.txt",ios::out);
int x;


int array[1000];
int i=0;
while (fin >> x)
{    
    array[i] = x;
    cout << array[i] << " ";
    fout << array[i] << " ";
    i++;
}


return 0;
}
I didn't understand the for part as you didn't use any { } and only one statement was effectively IN the for: array[i] = x; Also I don't understand how you could get away with cout << array[i] << " "; since i is declared in the for, which, in the absence of the {} has already ended. Unless you are using something like borland turbo 3.0 which, to my knowledge allows such things (something that really sucks).
Hope this gets you on the right track. Cheers
 
Old 02-20-2008, 01:35 AM   #3
cyborgt
LQ Newbie
 
Registered: Feb 2008
Posts: 5

Original Poster
Rep: Reputation: 0
wow, dude, I can't believe you replied me soooo fast, thank you so much for your help, this was part of my homework, after 3 hours of research, i done the home work by myself(i can't even believe it by myself). I am posting it in case someone run into similar problem. BTW, i switched to another way to do it.

the Question is:
read a n.TXT which had 2 numbers(20 30) at first row, indicate rows and columes.
then it will be 20 x 30 numbers with space in between. My professor asked to do this using dyanmic array so array[1000] can't be use. then calculate the number that read, if it = 0, output 0, otherwise output 1, and use same format output the array to a graphic.TXT file.
this was what i did.....

#include "stdafx.h"
#include <iostream>
#include <stdio.h>

int main()
{
int getsize[2];
FILE * pFile;
pFile = fopen ("n.txt","r");

for (int i=0; i<2; i++)
fscanf (pFile, "%d", &getsize[i]);

int* graphic =NULL;
int size = getsize[0]*getsize[1];
int change = getsize[1] - 1;
graphic = new int[size];
for (int t=0; t<size; t++)
graphic[t] = 0;

for (int c=0; c<size; c++)
{
fscanf (pFile, "%d", &graphic[c]);
if(graphic[c]==0)
graphic[c] = 0;
else
graphic[c] = 1;
}
fclose (pFile);
pFile = fopen ("graphic.txt","w+");
for (int n=0 ; n<size ; n++)
{
fprintf (pFile, "%d", graphic[n]);
fprintf (pFile, " ");

if (n == change)
{
fprintf(pFile, "\n");
change = change + getsize[1];
}
}
fclose (pFile);
return 0;
}

again, thank you NC3B!!! you rocks~~~


Quote:
Originally Posted by nc3b View Post
You're almost there
You need to :
1. use the code tags
2.
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <strstream>

using namespace std;

int main()
{


ifstream fin("c:\\n.txt",ios::in);
ofstream fout("c:\\m.txt",ios::out);
int x;


int array[1000];
int i=0;
while (fin >> x)
{    
    array[i] = x;
    cout << array[i] << " ";
    fout << array[i] << " ";
    i++;
}


return 0;
}
I didn't understand the for part as you didn't use any { } and only one statement was effectively IN the for: array[i] = x; Also I don't understand how you could get away with cout << array[i] << " "; since i is declared in the for, which, in the absence of the {} has already ended. Unless you are using something like borland turbo 3.0 which, to my knowledge allows such things (something that really sucks).
Hope this gets you on the right track. Cheers
 
Old 02-20-2008, 01:44 AM   #4
cyborgt
LQ Newbie
 
Registered: Feb 2008
Posts: 5

Original Poster
Rep: Reputation: 0
I am sorry i forgot the comments.

#include "stdafx.h" //without this, my VC++ gave me error......
#include <iostream> //need this
#include <stdio.h> //must have this

int main()
{
int getsize[2]; //get info from first row about size
FILE * pFile; //some magic things
pFile = fopen ("n.txt","r"); //open n.txt "r" means file must exist

for (int i=0; i<2; i++) //only need 2 numbers
fscanf (pFile, "%d", &getsize[i]); //get first 2 into size array

int* graphic =NULL; //preparing new dynamic array
int size = getsize[0]*getsize[1]; //get size of file
int change = getsize[1] - 1; // get the point which change line
graphic = new int[size]; //new dynamic arry
for (int t=0; t<size; t++)
graphic[t] = 0;

for (int c=0; c<size; c++)
{
fscanf (pFile, "%d", &graphic[c]); //read numbers into arry
if(graphic[c]==0) // do the calculation
graphic[c] = 0;
else
graphic[c] = 1;
}
fclose (pFile); //close n.txt
pFile = fopen ("graphic.txt","w+"); //open new graphic.txt
for (int n=0 ; n<size ; n++)
{
fprintf (pFile, "%d", graphic[n]); //output array to file
fprintf (pFile, " "); //stupid way to insert space in between

if (n == change) //find the colume limit and insert change line
{
fprintf(pFile, "\n");
change = change + getsize[1];
}
}
fclose (pFile);
return 0;
}
 
Old 02-20-2008, 02:08 PM   #5
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
You should really use [code] tags when posting here, as nc3b suggested. Otherwise you lose all indentation.
 
Old 02-20-2008, 02:11 PM   #6
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Hmm well I'm not sure if it's ok to post homework solutions here, but then again, maybe it's just funny-looking posting questions and hints, but not the end solution.

Hopefully if somebody has a similar homework he tries to study and understand the code in addition to copy-pasting it.
 
  


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
invalid types ‘int[int]’ for array subscript medha Programming 16 08-25-2006 08:30 AM
Reading from a txt file into a two dimension array in C kponenation Programming 3 11-26-2005 07:04 PM
passing int array to thread? Thinking Programming 2 09-21-2005 11:00 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM
Char array to int without losing value ? Dimitris Programming 3 01-14-2004 12:08 PM


All times are GMT -5. The time now is 06:00 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration