problem - read file to an array

Hey! Im trying to read a file, row by row, into an array. I do suceed,but when i run the program, it crashes.

It seems to be a trouble with this line:"in_stream.getline(rad,250,'\n');"

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include<fstream>
#include<ostream>
#include<string>
using namespace std;
 
int main()
{

ifstream  in_stream;
in_stream.open("cd.txt");


if ( !in_stream )

{ 
	cout<<"Kan inte l\x84sa fr\x86n cd.txt"<<endl;
	return 1;
}

char *rad;
rad = new char;

string CD[7];



for(int i = 0;i<7 ; i++)
{
    in_stream.getline(rad,250,'\n');

	CD[i]= rad;
	cout<<CD[i]<<endl;
}
in_stream.close();

return 0;
}


Does anyone know how to solve this?
You are trying to read 250 characters (maximum) into 1 character. Fix it thus:

  22 rad = new char[250];

Also, don't forget to delete[] rad; before you terminate your program.


I would be remiss, however, if I didn't ask why you don't just read directly to the strings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
  ifstream in_stream("cd.txt");

  if (!in_stream)
  {
    cout << "Fooey!\n";
    return 1;
  }

  string CD[7];

  for (int i=0; i<7; i++)
  {
    getline(in_stream,CD[i]);
    cout << CD[i] << endl;
  }

  in_stream.close();

  return 0;
}

Hope this helps.
It sure fixed the problem! Thank you so much!
I'm doing the same thing now, but with an char array instead. This code gives me a syntax error, so do you know if there's a easy way to solve this or is there much that needs to be fixed?

error C2059: syntax error : ']'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include<fstream>
#include<ostream>
#include<string>
using namespace std;

sortera();
 
int main()
{

ifstream  in_stream;
in_stream.open("cd.txt");


if ( !in_stream )

{ 
	cout<<"Kan inte l\x84sa fr\x86n cd.txt"<<endl;
	return 1;
}


char *skivor[40];
char rad[250];
int antal=0;


/*for(int i = 0;i<7 ; i++)
{
    in_stream.getline(rad,250,'\n');

	skivor[i]= rad;

}*/

while(!in_stream.eof())
{
     in_stream.getline(rad,250,'\n');
     skivor[antal] = new char[strlen(rad)+1];
     strcpy(skivor[antal],rad);
     antal++;
}



in_stream.close();

sortera();

ofstream out_stream("newCD.txt"); // skapar en ny fil
out_stream<<
"Sorterade CD-skivor:\n"<<endl<<
skivor[6]<<endl<<
skivor[5]<<endl<<
skivor[4]<<endl<<
skivor[3]<<endl<<
skivor[2]<<endl<<
skivor[1]<<endl<<
skivor[0]<<endl;


return 0;
}

sortera(char skivor[][80], int antal)
{
	for(int j=0; j<antal; j++)
	{
		if ( skivor[j]< skivor[j+1])
		{
			swap(skivor[j+1],skivor[j]);
		}
		
	}

	for(int k=0;k<antal;k++)
	{
		cout<<skivor[k]<<endl;
	}

	return antal;
	return skivor[][80];
}


thanks!
What's up with line 83 and 84? You can never return multiple variables from a function (unless you create an object or array to hold the values in question).
Secondly there is an STL sort algo in the <algorithm> header; you'd have an easier time just using that rather than your bubble sort. You pass it pointers to the start and one after the end, I believe.
Third, why not just create an array of strings rather than an array of dynamically allocated char[]? Or even better use vector. MD arrays don't pass around too well.
Now then which line is your issue on? All your subscripts look fine.
Hm, do I even need to send back a return value at the "sortera" algorithm?

Ok, but how is that done with the char array?

I cannot use a String, because the task tell me to make it of a char or dynamically allocated char, thats why.

the error is on row 83. If i deleate one of the return values, or both,I get this:

1
2
3
4
5
6
7
8
C:\Program Files\Microsoft Visual Studio\MyProjects\Labb_5\labb_5.cpp(82) : warning C4508: 'sortera' : function should return a value; 'void' return type assumed
c:\program files\microsoft visual studio\vc98\include\xutility(100) : error C2075: '_Tmp' : array initialization needs curly braces
        C:\Program Files\Microsoft Visual Studio\MyProjects\Labb_5\labb_5.cpp(72) : see reference to function template instantiation 'void __cdecl std::swap(char (&)[80],char (&)[80])' being compiled
c:\program files\microsoft visual studio\vc98\include\xutility(101) : error C2106: '=' : left operand must be l-value
        C:\Program Files\Microsoft Visual Studio\MyProjects\Labb_5\labb_5.cpp(72) : see reference to function template instantiation 'void __cdecl std::swap(char (&)[80],char (&)[80])' being compiled
c:\program files\microsoft visual studio\vc98\include\xutility(101) : error C2106: '=' : left operand must be l-value
        C:\Program Files\Microsoft Visual Studio\MyProjects\Labb_5\labb_5.cpp(72) : see reference to function template instantiation 'void __cdecl std::swap(char (&)[80],char (&)[80])' being compiled
Error executing cl.exe.
Last edited on
The problem is that you did not specify a return type for sortera().

In C++ you can not normally declare a function without a return type.[1]

This is what you did:
sortera(); // sortera() is a function that takes no arguments and which might return any or no type.

This is what should be done:
int sortera(); // sortera() is a function that takes no arguments and returns an int.
or you could say
void sortera(); // sortera() is a function that takes no arguments and returns nothing.

The reason why? C++ is a strongly typed language, which works very hard to make sure you do not make undefined type conversions. Your original function would break this system. For instance, you could create a pointer that points to some unknown location in memory (a Bad Thing). This is why the compiler returns an error.

In this particular case, I would guess that the compiler saw that your function lacked a return type, put the type void (nothing returned) and then got very surprised when you did actually return an int (and a char** in this case :)

[1] A class destructor or constructor is always declared with no return type, not even void. This is not relevant in this case, however.
Topic archived. No new replies allowed.