reading file data into array

Hi
Im trying to read a line of integers from a file separated by tabs into a integer array

[code]
#include
#include
#include
#include

using namespace std;

int main(){
char str[100];

ifstream infile( "test.txt", ios::in);
if(!infile){
cerr<<"crud";
return 1;
}
infile.getline(str,100);
cout<<str; // works fine

char * pch;
pch = strtok (str," ");
while (pch != NULL){
printf ("%s
",pch);
pch = strtok (NULL, " ,.");
} // works fine
int data[25];
int i = 0;
while (pch != NULL){
if(i!=25){
pch = strtok (NULL, " ,.");
data[i]=atoi(pch);
}
i++;
}
for (int j=0; j<25; j++){
cout<<data[i]<<" "; // problem here output is 25 0's
}


return 0;
}
[/code]

any help would be appreciated

Comments

  • : Hi
    : Im trying to read a line of integers from a file separated by tabs into a integer array
    :
    : [code]
    : #include
    : #include
    : #include
    : #include
    :
    : using namespace std;
    :
    : int main(){
    : char str[100];
    :
    : ifstream infile( "test.txt", ios::in);
    : if(!infile){
    : cerr<<"crud";
    : return 1;
    : }
    : infile.getline(str,100);
    : cout<<str; // works fine
    :
    : char * pch;
    : pch = strtok (str," ");
    : while (pch != NULL){
    : printf ("%s
    ",pch);
    : pch = strtok (NULL, " ,.");
    : } // works fine
    : int data[25];
    : int i = 0;
    [red]strtok() has already reached the end of the string here. So all
    further attempts to use the string will fail. You need to combine
    the loop below and the loop above into one loop.[/red]
    : while (pch != NULL){
    : if(i!=25){
    : pch = strtok (NULL, " ,.");
    : data[i]=atoi(pch);
    : }
    : i++;
    : }
    : for (int j=0; j<25; j++){
    : cout<<data[i]<<" "; // problem here output is 25 0's
    : }
    :
    :
    : return 0;
    : }
    : [/code]
    :
    : any help would be appreciated
    :

  • : : Hi
    : : Im trying to read a line of integers from a file separated by tabs into a integer array
    : :
    : : [code]
    : : #include
    : : #include
    : : #include
    : : #include
    : :
    : : using namespace std;
    : :
    : : int main(){
    : : char str[100];
    : :
    : : ifstream infile( "test.txt", ios::in);
    : : if(!infile){
    : : cerr<<"crud";
    : : return 1;
    : : }
    : : infile.getline(str,100);
    : : cout<<str; // works fine
    : :
    : : char * pch;
    : : pch = strtok (str," ");
    : : int data[25];
    : : int i = 0; [red] i got rid of the other loop and now [/red]
    : : while (pch != NULL){
    : : if(i!=25){
    : : pch = strtok (NULL, " ,.");
    : : data[i]=atoi(pch); [red] this fails [/red]
    : : }
    : : i++;
    : : }
    : : for (int j=0; j<25; j++){
    : : cout<<data[i]<<" "; // problem here output is 25 0's
    : : }
    : :
    : :
    : : return 0;
    : : }
    : : [/code]
    : :
    : : any help would be appreciated
    : :
    :
    :
    the error is

    Unhandled Exception: System.NullReferenceException: Object reference not set to
    an instance of an object.
    at atoi(SByte* )
  • [code]
    : : : pch = strtok (NULL, " ,.");
    : : : data[i]=atoi(pch); [red] this fails [/red]
    [/code]

    The above two lines are in reverse order. switch them around.
  • : [code]
    : : : : pch = strtok (NULL, " ,.");
    : : : : data[i]=atoi(pch); [red] this fails [/red]
    : [/code]
    :
    : The above two lines are in reverse order. switch them around.
    :

    well it doesnt break now but atoi still doesnt do anything
    output
    0 /t 201 /t 17 /t 142 /t 79 /t 295 /t 300 /t 293 /t 276 /t 293 /t 227 /t 126 /t 39 /t 121 /t 288 /t 192 /t 613 /t 264 /t 226/t 199/t 427 /t 218 /t 26/t 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    I put the /t to indicate where the tabs are


  • [b][red]This message was edited by stober at 2005-5-1 11:4:43[/red][/b][hr]
    : : [code]
    : : : : : pch = strtok (NULL, " ,.");
    : : : : : data[i]=atoi(pch); [red] this fails [/red]
    : : [/code]

    atoi() works greate. When you pass sh$t to it atoi will just return in like kind.

    If the numbers are separated by tabs, then strtok() line above is incorrect. Do the numbers in the file really contain spaces, commas and periods??


  • : [b][red]This message was edited by stober at 2005-5-1 11:4:43[/red][/b][hr]
    : : : [code]
    : : : : : : pch = strtok (NULL, " ,.");
    : : : : : : data[i]=atoi(pch); [red] this fails [/red]
    : : : [/code]
    :
    : atoi() works greate. When you pass sh$t to it atoi will just return in like kind.
    :
    : If the numbers are separated by tabs, then strtok() line above is incorrect. Do the numbers in the file really contain spaces, commas and periods??
    :
    :
    lol yeah that was pretty stupid dunno why i still still had that
    the new code looks like this
    [code]
    while (pch != NULL){
    if(i!=25){
    data[i]=atoi(pch);
    pch = strtok (NULL, " ");
    }
    i++;
    }
    [/code]
    yet the output is still the same
    :
  • : Hi
    : Im trying to read a line of integers from a file separated by tabs into a integer array
    :
    : [code]
    : #include
    : #include
    : #include
    : #include
    :
    : using namespace std;
    :
    : int main(){
    : char str[100];
    :
    : ifstream infile( "test.txt", ios::in);
    : if(!infile){
    : cerr<<"crud";
    : return 1;
    : }
    : infile.getline(str,100);
    : cout<<str; // works fine
    :
    : char * pch;
    : pch = strtok (str," ");
    : while (pch != NULL){
    : printf ("%s
    ",pch);
    : pch = strtok (NULL, " ,.");
    : } // works fine
    : int data[25];
    : int i = 0;
    : while (pch != NULL){
    : if(i!=25){
    : pch = strtok (NULL, " ,.");
    : data[i]=atoi(pch);
    : }
    : i++;
    : }
    : for (int j=0; j<25; j++){
    : cout<<data[i]<<" "; // problem here output is 25 0's
    : }
    :
    :
    : return 0;
    : }
    : [/code]
    :
    : any help would be appreciated
    :

    [blue]The stream extraction operator (>>) will skip over any whitespace (tab's are included in this) automatically. All you should need is this:

    [code]
    #include
    #include

    using namespace std;

    int main(){

    ifstream infile( "test.txt");
    if(!infile){
    cerr<<"crud";
    return 1;
    }
    int data[25];
    [blue]for( int i = 0; i < 25; ++i){
    infile >> data[i];
    }[/blue]

    for (int j = 0; j < 25; ++j){
    cout<<data[[red]j[/red]]<<" "; // was data[[red]i[/red]]
    }

    return 0;
    }
    [/code]
    [/blue]
  • : : Hi
    : : Im trying to read a line of integers from a file separated by tabs into a integer array
    : :
    : : [code]
    : : #include
    : : #include
    : : #include
    : : #include
    : :
    : : using namespace std;
    : :
    : : int main(){
    : : char str[100];
    : :
    : : ifstream infile( "test.txt", ios::in);
    : : if(!infile){
    : : cerr<<"crud";
    : : return 1;
    : : }
    : : infile.getline(str,100);
    : : cout<<str; // works fine
    : :
    : : char * pch;
    : : pch = strtok (str," ");
    : : while (pch != NULL){
    : : printf ("%s
    ",pch);
    : : pch = strtok (NULL, " ,.");
    : : } // works fine
    : : int data[25];
    : : int i = 0;
    : : while (pch != NULL){
    : : if(i!=25){
    : : pch = strtok (NULL, " ,.");
    : : data[i]=atoi(pch);
    : : }
    : : i++;
    : : }
    : : for (int j=0; j<25; j++){
    : : cout<<data[i]<<" "; // problem here output is 25 0's
    : : }
    : :
    : :
    : : return 0;
    : : }
    : : [/code]
    : :
    : : any help would be appreciated
    : :
    :
    : [blue]The stream extraction operator (>>) will skip over any whitespace (tab's are included in this) automatically. All you should need is this:
    :
    : [code]
    : #include
    : #include
    :
    : using namespace std;
    :
    : int main(){
    :
    : ifstream infile( "test.txt");
    : if(!infile){
    : cerr<<"crud";
    : return 1;
    : }
    : int data[25];
    : [blue]for( int i = 0; i < 25; ++i){
    : infile >> data[i];
    : }[/blue]
    :
    : for (int j = 0; j < 25; ++j){
    : cout<<data[[red]j[/red]]<<" "; // was data[[red]i[/red]]
    : }
    :
    : return 0;
    : }
    : [/code]
    : [/blue]
    :


    awesome thanks
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories