LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   plz need help on C++ about read int from a TXT to array. (http://www.linuxquestions.org/questions/programming-9/plz-need-help-on-c-about-read-int-from-a-txt-to-array-622394/)

cyborgt 02-19-2008 10:47 PM

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;
}

nc3b 02-20-2008 12:44 AM

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:)

cyborgt 02-20-2008 01:35 AM

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 (Post 3063361)
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:)


cyborgt 02-20-2008 01:44 AM

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;
}

Quigi 02-20-2008 02:08 PM

You should really use [code] tags when posting here, as nc3b suggested. Otherwise you lose all indentation.

b0uncer 02-20-2008 02:11 PM

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.


All times are GMT -5. The time now is 02:49 AM.