0
////////// new update!!!!! //////////

.txt have 15 numbers , last number is "15" 1. i try to count how many(save into my index) of numbers in my .txt file. 2. create my dynamic array size with the index. 3. save all the number into my dynamic array.

question: how to cover the char dynamic array to int dynamic array.

i got garbage output in my terminal:

Open sucessues!!  
index: 15
buffer: 15
0
1073741824
0
1073741824
2136670223
32767
-1680479188
32767
0
0
0
0
0
0
0

int main(int argc, char* argv[])
{

char *nPtr = argv[1];
char *buffer = new char[5];
int index = 0;



ifstream fin(nPtr); //open the file
if (argc > 1){

// allocate the memory
if(!fin){

cout << "can't read the file!!" << endl;  
return -1;
}

if(fin){

    cout << "Open sucessues!! " << endl;
}




while (!fin.eof()){

fin >> buffer;
index++; //counting here!!!
}

cout << "index: " << index << endl; //print out the counting results!
cout << "buffer: " << buffer << endl; // checking the last number!  should "15"

delete[] buffer; // 
buffer = NULL;

int *number = new int[index]; 
char *temp = new char[index];
int *home = number; //home

while(!fin.eof()){

    fin >> temp;
    *number= atoi(temp); //im confessing right here!!!
    number++;
    temp++;
}



number = home;

for (int i = 0; i < index; ++i)
{
    cout << *number << endl;  //*number print out garbage, i don't know why!
    number++;
}




fin.close( );
}

return 0;
}
/////************

//////// old ///////// don't read /////i am wondering how to use argc and argv to read a file: numbers.txt(few numbers inside). my goal is: read a file with my ./sort in terminal like: ./sort numbers then use buffer and index to count how many number inside, use index to create dynamic array, finally i read the file again, but change all the "number" to int by using atoi.

i got the Segmentation fault: 11 after i type: ./sort numbers in my terminal.

can anyone help me here? i need those array to sort my number. here i got so far:

int main(int argc, char* argv[])
{ 
    char *nPtr = argv[1]; 
    char *buffer[3];
    int index = 0;


    ifstream fin(nPtr); //open the file


    // allocate the memory
    if(fin.is_open()){


    cout << "open" << endl;

            while(!fin.eof()){
                fin >> *buffer;
                index++;



            }

    cout << index << endl;
    }
7llllll
  • 13
  • 8

1 Answers1

0
char *buffer[3];

Creates an array of three pointers to characters. It does not allocate any storage to point at. It does not assign any storage to be pointed at. These pointer could be pointing at anything. Valid memory, invalid memory, your older brother's porn stash, you don't know. If you are lucky, they will point to invalid memory and your program will crash.

fin >> *buffer;

Attempts to place a string read from the file into the memory pointed at by first of the three pointers from above. Since we don't know where they point, we don't know where the input from the file will be written. Odds are pretty good that it will try to write into invalid memory and the program will crash.

To solve this, allocate some storage, point the pointer at this storage, and then read into the pointer.

eg.

char *buffer[3];
char storage[128];
buffer[0] = storage;

then later

fin >> *buffer;

That said, I don't think this is what you want at all. More likely

char *buffer[3];

should be

char buffer[3];

in which case

fin >> *buffer;

will read exactly one character from the file into buffer, so that's probably a typo as well and

fin >> buffer;

Is what is intended. WARNING!!! This will likely still crash if the string read from fin is longer than 2 characters. You probably want to rethink this in general.

If allowed use std::string and std::vector, but seeing as it is still early in the semester your instructor probably expects to teach you code by having you hit things with rocks and maybe rub twigs together to produce fire.

user4581301
  • 29,019
  • 5
  • 26
  • 45
  • YOU ARE the BEST in THIS WORLD and i want say I LOVE YOU – 7llllll Oct 03 '15 at 01:55
  • hi, still have a little trouble in here. i got 15 index here, and i put in into my int *number = new int[index]; by using while !fin.eof. how can i turn all the string number to int number by using atoi , i am thinking that save it into buffer first, atoi(buffer); and *number = buffer. can i do this way or not? – 7llllll Oct 03 '15 at 02:10
  • i got the out put like this: Open sucessues!! index: 15 0 -268435456 0 -268435456 2136670224 32767 -1680479189 32767 2136686792 32767 -1680479188 32767 0 0 0 xi – 7llllll Oct 03 '15 at 02:16
  • @stacker Post a new question with the new code. 2 reasons 1) adding more pieces muddies this question and 2) I have no clue what your code looks like now. – user4581301 Oct 03 '15 at 03:41