943,769 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 6306
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 2nd, 2009
0

Read number in txt file into an Array

Expand Post »
Hi,

I'm a newbie in C++ and i'm working on a project, but it doesn't work..

The C++ program should read numbers into an array, the numbers are in a txt file.
Each array number : ex. array[1], array[2], array[3]... should get a number from the txt file.
In the file, for each new number, a new line is started.

Can someone help me with this ?

Sven
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swemp is offline Offline
20 posts
since Dec 2008
Jan 2nd, 2009
0

Re: Read number in txt file into an Array

fstream header will do. just include <ifstream>
then do
ifstream vname("filename");
vname >> array[i]; as many times as needed
Reputation Points: 36
Solved Threads: 5
Newbie Poster
da penguin is offline Offline
18 posts
since Dec 2008
Jan 2nd, 2009
0

Re: Read number in txt file into an Array

Note that c++ arrays are 0 based. That is, array[0] is the first element, not array[1].

Dave
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jan 2nd, 2009
0

Re: Read number in txt file into an Array

What, exactly, doesn't work? The input portion?
Once you input the data, have you outputted it to check if it is wrong or right?

Perhaps you could post the code you have already written, to give us something to work with.
Reputation Points: 33
Solved Threads: 9
Junior Poster
DavidB is offline Offline
188 posts
since Jul 2006
Jan 2nd, 2009
0

Re: Read number in txt file into an Array

Click to Expand / Collapse  Quote originally posted by daviddoria ...
Note that c++ arrays are 0 based. That is, array[0] is the first element, not array[1].

Dave
I know that, but I'm using a for loop..
for (i=1; i<=number; i++)
and array[i]

Or isn't that correct ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swemp is offline Offline
20 posts
since Dec 2008
Jan 2nd, 2009
0

Re: Read number in txt file into an Array

nope, if you make an array of length 5

C++ Syntax (Toggle Plain Text)
  1. int test[5];

then you have to use elements 0 to 4
C++ Syntax (Toggle Plain Text)
  1. for (i=0; i<5; i++)
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jan 2nd, 2009
0

Re: Read number in txt file into an Array

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void drukintro ();
  7. void geef_rij( int& aantal, int getallen[]);
  8. void kies_bestand (int aantal, int rij[]);
  9. void lees_bestand (char bestandnaam[], int aantal, int bestandrij[]);
  10. void sorteerint(int aantal, int rij[]);
  11. void printintrij(int aantal, int rij[]);
  12.  
  13. void drukintro ()
  14. {
  15. int i;
  16. cout << "Dit is een programma dat een rij van getallen sorteert" << endl
  17. << "en het voorkomen van elk getal telt," << endl
  18. << "en daarna van hoog naar laag op het scherm drukt." << endl;
  19. for ( i=0; i<=54; i++)
  20. {
  21. cout << "-";
  22. }
  23. cout << endl << endl;
  24.  
  25. }
  26.  
  27. void geef_rij( int& aantal, int getallen[])
  28. {
  29. int i, eigen_getal;
  30. cout << "How many numbers do you want to enter ?" << endl;
  31. cin >> aantal;
  32.  
  33. for (i=0; i<aantal; i++)
  34. {
  35. cin >> eigen_getal;
  36. getallen [i]= eigen_getal;
  37. }
  38. }
  39.  
  40. void kies_bestand (int aantal, int rij[])
  41. {
  42. int keuze_lijst;
  43.  
  44. cout << "Which list do you want to use ?" << endl
  45. << "For list 1, type 1" << endl
  46. << "For list 2, type 2" << endl
  47. << "For list 3, type 3" << endl << endl;
  48. cout << "Keuze : ";
  49. cin >> keuze_lijst;
  50.  
  51. if (keuze_lijst < 1 || keuze_lijst > 3 )
  52. {
  53. cout << "This list doesn't exist !" << endl;
  54. exit(1);
  55. }
  56.  
  57. if (keuze_lijst == 1)
  58. {
  59. lees_bestand ("lijst1.txt", aantal, rij);
  60. }
  61.  
  62. if (keuze_lijst == 2)
  63. {
  64. lees_bestand ("lijst2.txt", aantal, rij);
  65. }
  66.  
  67. if (keuze_lijst == 3)
  68. {
  69. lees_bestand ("lijst3.txt", aantal, rij);
  70. }
  71. }
  72.  
  73. void lees_bestand (char bestandnaam[], int aantal, int bestandrij[])
  74. {
  75. ifstream bestand;
  76. int i=0;
  77.  
  78. bestand.open(bestandnaam);
  79. if ( ! bestand.fail() )
  80. {
  81. do
  82. {
  83. bestand >> bestandrij[i];
  84. i++;
  85. }while (i<aantal);
  86. bestand.close();
  87. }
  88.  
  89. else if (bestand.fail())
  90. {
  91. cout << "Opening of txt file failed ! \n";
  92. exit(1);
  93. }
  94. }
  95.  
  96.  
  97. void sorteerint(int aantal, int rij[])
  98. {
  99. int i,j,temp;
  100. for (i=0; i<aantal-1; i++)
  101. for (j=aantal-1; j>i; j--)
  102. if (rij[j-1]>rij[j])
  103. {
  104. temp=rij[j];
  105. rij[j]=rij[j-1];
  106. rij[j-1]=temp;
  107. }
  108. }
  109.  
  110. void printintrij(int aantal, int rij[])
  111. {
  112. int i;
  113. for (i=0; i<aantal; i++)
  114. cout << rij[i] << " ";
  115. cout << endl;
  116. }
  117.  
  118.  
  119. int main ()
  120. {
  121. int eigen_rij[50], aantal_getallen;
  122. char antwoord;
  123.  
  124. drukintro ();
  125. cout << "Do you want to give in an array of whole numbers yourself" << endl
  126. << "or read from an array of whole numbers from a file ? (Y / F )" << endl;
  127. cin >> antwoord;
  128. cout << endl;
  129.  
  130. if ( antwoord == 'Y' || antwoord == 'y' )
  131. {
  132. geef_rij ( aantal_getallen, eigen_rij );
  133. cout << endl;
  134. }
  135.  
  136. if ( antwoord == 'F' || antwoord == 'f' )
  137. {
  138. kies_bestand( aantal_getallen, eigen_rij );
  139. cout << endl;
  140. }
  141.  
  142. else
  143. {
  144. exit (1);
  145. }
  146.  
  147. sorteerint ( aantal_getallen, eigen_rij );
  148. cout << "Sorted array : ";
  149. printintrij( aantal_getallen, eigen_rij );
  150. }

I've translated some of the lines of my original program, because there the text that you can read on the screen is Dutch, I'm from Belgium you know..

If you test it out... You'll see that the array doesn't read in one of the txt-files..
Last edited by Swemp; Jan 2nd, 2009 at 7:23 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swemp is offline Offline
20 posts
since Dec 2008
Jan 2nd, 2009
0

Re: Read number in txt file into an Array

Click to Expand / Collapse  Quote originally posted by Swemp ...
I've translated some of the lines of my original program, because there the text that you can read on the screen is Dutch, I'm from Belgium you know..

If you test it out... You'll see that the array doesn't read in one of the txt-files..
We can't test it out if you don't supply the input text files. Which text file doesn't read in?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,373 posts
since Jan 2008
Jan 2nd, 2009
0

Re: Read number in txt file into an Array

We can't test it out if you don't supply the input text files. Which text file doesn't read in?
It doesn't read any of them..

list1 :
15
78
32
15
74
15
96
32
159
-1
96
20
25
45
38
95
18
20
98
159
78
15

list 2 :
58
78
12
13
95
147
258
369
159
753
58
95
95
14
78
62
32
14
159
842
24
75
62
314
789
15
78
321
8
6
7

list3:
45
78
97
12
14
18
32
0
-1
-25
87
26
17
26
78
45
12
18
45
65
32
-25
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swemp is offline Offline
20 posts
since Dec 2008
Jan 2nd, 2009
0

Re: Read number in txt file into an Array

Click to Expand / Collapse  Quote originally posted by Swemp ...
It doesn't read any of them..

Don't assume that a run-time error means that the program isn't reading from the files. Place some cout statements inside the function that reads from the file that display the parameters passed to the function and display what is being read in. Make sure that the parameters are what you want them to be, and that what you want to read in is actually being stored how you want it to be stored, and that you are going through the loop the correct number of times.
Last edited by VernonDozier; Jan 2nd, 2009 at 9:12 pm. Reason: Improved grammar
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,373 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Automatic implicit type conversions
Next Thread in C++ Forum Timeline: Array using ** pointers?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC