Jump to content

Reading from file char by char in array

- - - - -

  • Please log in to reply
7 replies to this topic

#1
cypro

cypro

    Newbie

  • Members
  • Pip
  • 5 posts
Hello i need an algorithm to do the following:
I have a text file:

5 32
1 23

And i want to save each number in array but the first column in one array and the second column in second array

e.g array1 = 5, 1
array2 = 32, 23

Thanks.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What do you have so far? Where are you stuck?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts
It could look like this:

#include <stdio.h>


#define LENGTH_OF_FILE 2


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

{

    int array1[LENGTH_OF_FILE];

    int array2[LENGTH_OF_FILE];


    FILE* ptrFile = fopen("File.txt", "r");


    for(int a = 0; a < LENGTH_OF_FILE; a++)

    {

        fscanf(ptrFile, "%d %d", &array1[a], &array2[a]);

    }


    fclose(ptrFile);


    return 0;

}


Look here for further information.

Greets,
artificial
Sometimes words ain't enough to express something. That's why computer scientists use double words.

#4
cypro

cypro

    Newbie

  • Members
  • Pip
  • 5 posts
I forget to say that my program is C++.

here is my code of reading full line
#include <iostream>

#include <string>

#include <fstream>


using namespace std;


int main()

{


ifstream input ("input.txt", ios::in);

ofstream output("output.txt", ios::out);


int x;


while(!input.eof())

{

	

	input >> x;

	

}

input.close();

return 0;}


#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
But C code should work fine under C++, shouldn't it?

I think the scanf() method should work.

#6
haltox

haltox

    Newbie

  • Members
  • PipPip
  • 29 posts
Only the include should be a little different. I think it's cstdio.h.

#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
No, and no.

Here's a way to do this, and the only real issue is that you're not putting it into an array yet. O_o

while (!input.eof())
{
  while (input)
  {
    // I do these getline antics to ensure that the input is PER LINE rather than
    // just a bunch of ints in a row. 
    std::string str;
    getline(input, str);
    std::stringstream strm(str);

    // Then we extract the formatted data (so we don't have to parse it on our own)
    strm >> x;
    first_vector.push_back(x);

    if (input)
    {
       // Yes, another painful if statement to make sure the last formatted extraction
       // didn't fail.
       strm >> x;
       second_vector.push_back(x);
    }
  }

  if (input.fail())
  {
    input.clear(std::ios_base::failbit);
    std::cerr << "Bad input format on line " << line_num;
  }
}

Hmm... I think that should work. :P
Wow I changed my sig!

#8
cypro

cypro

    Newbie

  • Members
  • Pip
  • 5 posts
Ok thanks :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users