14 Replies - 729 Views - Last Post: 26 June 2011 - 09:49 PM Rate Topic: -----

#1 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

How do I Go About reading 1 number at a time from a Data File?

Posted 25 June 2011 - 02:52 PM

The title is self explanatory. how would i do this?
Is This A Good Question/Topic? 0
  • +

Replies To: How do I Go About reading 1 number at a time from a Data File?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 25 June 2011 - 03:13 PM

How would you read 1 number at a time from the console?

EDIT: I realize this is a bit confusing, but this forum is not for help questions of this sort. Use the main forum C and C++ forum for questions.

This post has been edited by Oler1s: 25 June 2011 - 03:14 PM

Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,676
  • Joined: 27-December 08

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 25 June 2011 - 03:42 PM

Moved to C/C++ Help.
Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,350
  • Joined: 25-December 09

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 25 June 2011 - 03:46 PM

Do you know how to open a file? Do you know how to read anything from a file?

Post some code of your best try at reading a file and ask specific questions about the posted code.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 gsapienza  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 17-June 11

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 25 June 2011 - 07:02 PM

Once you open the file successfully you can do this

ifstream infile;

int array[10];
int number= 0;

for (int i=0; 10>i; i++) {
infile >> array[i];
}



Just to give you an idea of how it would mostly look.
Was This Post Helpful? 1
  • +
  • -

#6 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 25 June 2011 - 09:24 PM

View Postjimblumberg, on 25 June 2011 - 03:46 PM, said:

Do you know how to open a file? Do you know how to read anything from a file?

Post some code of your best try at reading a file and ask specific questions about the posted code.

Jim

well, this is my best shot....
/*Header Files
==============*/
#include <iostream> 	//Required for i/o
#include <iomanip>  	//Required for i/o Manipulators
#include <string>       //Required for Strings
#include <cstdlib>      //Required for System Commands
#include <cmath>	   	//Required for Math Library Functions
#include <fstream>      //Required for ifstream && ofstream
#include <cctype>		/*Required for Character
						Analization Library Functions*/
using namespace std;
/*Programmer: 	Ty Meador
Name:
Purpose:

*/
/*Globals
========= */
//double PI = 3.1415926535897

/*Function Prototypes
======================*/




int main()
{
    int temp, current_high = 0, sum = 0;
    float array[1001];
    ifstream file;
    string _1000num;
    file.open("1000.txt",ios::in);
    for(int i = 0; i < 1001; i++)
    {
        file >> array[i];
    }
    cout<<array[1000];
   if(sum > current_high)
    {
        current_high = temp;
    }
    //cout<<current_high;
    file.close();
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

#7 HcoJustin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 8
  • Joined: 26-June 11

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 26 June 2011 - 12:11 AM

#include <stdio.h>

//...

FILE in_file = fopen("in.txt", "r");
for(int i = 0; i < 1000; i++) {
    fscanf(in_file, "%f", &array[i]);
}
fclose(in_file);

//...




Easy enough?
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,350
  • Joined: 25-December 09

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 26 June 2011 - 05:04 AM

Quote

well, this is my best shot....


Ok lets start with the following snippet:

    int temp, current_high = 0, sum = 0;
    float array[1001];
    ifstream file;
    string _1000num;
    file.open("1000.txt",ios::in);
    for(int i = 0; i < 1001; i++)
    {
        file >> array[i];
    }



Everything here looks ok except you should always check that your file opened correctly and take action if it did not. Similar to the following:
// Check to insure file opened correctly.
   if(file)
   {
      cout << "Could not open the data file" << endl;
      return(1);
   }

The first line if(file) checks all the stream error flags and if none are set this will return false.

Next your for loop will try to read 1000 floats from your data file. This is fine if there are at least 1001 numbers in the file and the file is formatted something like:
100.1
29339.9
...
254.1

or
254.2 264.77 2335.25
245.577 68777.12 0.00014
.....

But if there are not at least 1001 numbers in your file what will happen to any array element past the number of numbers in the file?

A better way of reading a file with an unknown number of entries is to read the file into the variable in the while statement:
int number_of_entries = 0;
while(file >> array[number_of_entries])
{
   number_of_entries++;
   // If there are too many numbers exit the loop.
   if(number_of_entries > 1001;
   {
      break;
   }
}

The above code will read the file until it reaches the end of file, or some other read error occurs. number_of_entries++; increments this counter. So now after you read the file you know how many item you read. You can use this variable in later loops to limit yourself to the number of actual items.

Ok now lets look at the rest of your program:
    cout<<array[1000];
   if(sum > current_high)
    {
    
    current_high = temp;
    }
    //cout<<current_high;
    file.close();
    return 0;
}



The first line in the above code will try to print out element 1000 of your array, which is not an error because you defined your array with 1001 elements. But you probably want to print the entire contents of the array not just one element. You will need to loop (a for loop maybe?) through your array and print each element.

Next you are assigning the current_high variable with the value held in temp, but you have never assigned temp any value. To avoid this kind of hard to find bug you should always initialize your variables when you declare them.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6051
  • View blog
  • Posts: 23,483
  • Joined: 23-August 08

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 26 June 2011 - 06:47 AM

View PostHcoJustin, on 26 June 2011 - 03:11 AM, said:

#include <stdio.h>

//...

FILE in_file = fopen("in.txt", "r");
for(int i = 0; i < 1000; i++) {
    fscanf(in_file, "%f", &array[i]);
}
fclose(in_file);

//...




Easy enough?


Except that the OP is apparently using C++, not C, and therefore should use the C++ iostream library.
Was This Post Helpful? 0
  • +
  • -

#10 HcoJustin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 8
  • Joined: 26-June 11

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 26 June 2011 - 07:24 AM

View PostJackOfAllTrades, on 26 June 2011 - 06:47 AM, said:

Except that the OP is apparently using C++, not C, and therefore should use the C++ iostream library.


That was never established until he pieced together an example... I was just giving a viable alternative :)
Was This Post Helpful? 1
  • +
  • -

#11 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 26 June 2011 - 01:06 PM

From what i gather from what you all are telling me this *should* work. however i get this anomalie where the highest number is over two hundred. This code should find the highest sum of any 5 consecutive digits from a 1000 digit number located in the file "1000.txt". This means taht the highest possible (if the code were bug-free) would be 9*5 = 45.
/*Standard Header Files
=======================*/
#include <iostream>
#include <iomanip>
#include <fstream>
//#include <cassert>
#include <cctype>
//#include <cerrno>
//#include <cfloat>
//#include <ciso646>
#include <climits>
//#include <clocale>
#include <cmath>
//#include <csetjmp>
//#include <csignal>
//#include <cstdarg>
//#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
//#include <ctime>
using namespace std;
/*Programmer: 	Ty Meador
Name:
Purpose:
*/
/*Globals
========= */
//double PI = 3.1415926535897

/*Function Prototypes
======================*/




int main()
{
    unsigned int current_high = 0;
    ifstream file;
    string num;
    file.open("1000.txt",ios::in);
    file >> num;
    for(unsigned int i = 0; (i <= 995); i++)
    {
        unsigned int sum = 0;
        for(unsigned int i2 = 0; i2 < 5; i2++)
        {
            unsigned int i3 = i + i2;
            unsigned int temp = int(num[i3]);
            sum += temp;
        }
        if(sum > current_high)
        {
            current_high = sum;
        }
    }
    cout<<current_high;
    file.close();
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 26 June 2011 - 01:47 PM

num[i3] is a char, so the sum will be incorrect. In ASCII '0' has an integer value of 48.

50      unsigned int temp = int(num[i3]);


Was This Post Helpful? 1
  • +
  • -

#13 Ty Meador  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 23-May 11

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 26 June 2011 - 04:15 PM

View Post#define, on 26 June 2011 - 01:47 PM, said:

num[i3] is a char, so the sum will be incorrect. In ASCII '0' has an integer value of 48.

50      unsigned int temp = int(num[i3]);


i fixed this, but it is still incorrect according to P.E.
/*Standard Header Files
=======================*/
#include <iostream>
#include <iomanip>
#include <fstream>
//#include <cassert>
#include <cctype>
//#include <cerrno>
//#include <cfloat>
//#include <ciso646>
//#include <climits>
//#include <clocale>
#include <cmath>
//#include <csetjmp>
//#include <csignal>
//#include <cstdarg>
//#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
//#include <ctime>
using namespace std;
/*Programmer: 	Ty Meador
Name:
Purpose:
*/
/*Globals
========= */
//double PI = 3.1415926535897

/*Function Prototypes
======================*/




int main()
{
    unsigned int current_high = 0;
    ifstream file;
    string num;
    file.open("1000.txt",ios::in);
    file >> num;
    for(unsigned int i = 0; (i < 995); i++)
    {
        unsigned int sum = 0;
        for(unsigned int i2 = 0; i2 < 5; i2++)
        {
            unsigned int i3 = i + i2;
            unsigned int temp = (int (num[i3]) - 48);
            sum += temp;
        }
        if(sum > current_high)
        {
            current_high = sum;
        }
    }
    cout<<current_high;
    file.close();
    return 0;
}




and what i am trying to do is this...

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 26 June 2011 - 08:17 PM

I get the answer to life the universe and everything.

Are you reading the file ok?

Check the length of the string after you read from the file.

  cout << "string length " << num.length() << endl;


Was This Post Helpful? 2
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,350
  • Joined: 25-December 09

Re: How do I Go About reading 1 number at a time from a Data File?

Posted 26 June 2011 - 09:49 PM

The following will stop reading the file when it encounters the fist white space character.
file >> num;


The white space in this case is the end of line characters.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1