having trouble reading data from a file

This is a discussion on having trouble reading data from a file within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that will look in a file that contains data such as the following: ...

  1. #1
    Unregistered
    Guest

    having trouble reading data from a file

    I am trying to write a program that will look in a file that contains data such as the following:

    0.0000000e+000 1.0000000e+000
    9.8174770e-003 9.9518473e-001
    1.9634954e-002 9.8078528e-001
    2.9452431e-002 9.5694034e-001
    3.9269908e-002 9.2387953e-001

    and determine the number of rows of data. I seem to keep getting hung up on the while statement. Can anyone help?

    ------------------------------

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream.h>
    #include <fstream.h>

    void main(void)
    {
    char file_in[20];
    int insize=0, rows=0;
    double time;
    double amplitude;

    FILE *input;

    cout<<"Please enter the name of the file to load.\n";
    cin>>file_in;
    insize=strlen(file_in);
    input=fopen(file_in, "r");
    while(fscanf(input, "%lf %lf", &time, &amplitude)==2)
    rows++;
    cout<<"The number of rows is "<<rows
    }

  2. #2
    Unregistered
    Guest
    whoops, the paste didn't work ;-) The while line should read:

    while(fscanf(input, "%lf %lf", &time, &amplitude)==2)

  3. #3
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    cin.getline() should solve all of your problems.
    here is a program in fstream that will show all of the contents of the file, and display the number of rows:
    Code:
    #include <fstream.h>								//file I\O
    #include <iostream.h>								//guess?
    
    //MAIN////////////////////////////////////////////////
    int main(void){
    	char str[600];									//string array of 600
    	int rows=0;
    	ifstream file;
    	file.open("C:\\Poope.txt"); //open C:\Poope.txt
    	while (!file.eof())//the end of the file(eof)
    	{
    		file.getline(str, 90);						
    		cout << str << endl;			//outputs the first 90 characters of the file
    	rows++;
    	}
    	cout << "\n\n\nYour number of rows are: " << rows <<endl;
    	file.close();				//close the file
    	return(0);
    	}
    
    //END OF PROGRAM/////////////////////////////////////
    I hope you can build from that. This Program has no trouble with going to the next line, or printing a space and such.
    Last edited by Xterria; 11-07-2001 at 09:59 PM.

  4. #4
    Unregistered
    Guest
    That worked great, thanks a lot!

  5. #5
    Unregistered
    Guest
    I think I'm having a problem with what my pointers are referencing now. I'm supposed to add on to my previous assignment and copy the values from the input into an array and then copy the array elements into an output file. This program isn't generating an output file. Can anyone help?

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<iostream.h>
    #include<fstream.h>

    int main()
    {
    double **array; // 2d array of size array[rows][columns]
    int i, j; //indices
    int rows=0, columns=2; // Array dimensions
    double move;
    char file_in[20];
    char file_out[20];
    char str[600];
    FILE *input;
    FILE *output;

    cout<<"Please enter the name of the file to load.\n";
    cin>>file_in;
    strcpy(file_in, file_out,n);
    strcat(file_out,".gnu");
    input=fopen(file_in, "r");
    output=fopen(file_out, "w");

    ifstream file;
    file.open(file_in);
    while (!file.eof())
    {
    file.getline(str, 90);
    rows++;
    }

    /* Allocates memory for array */
    array = (double**) malloc (sizeof(double*)*rows);
    for(i=0;i < rows;i++)
    array[i] = (double*) malloc (sizeof(double)*columns);

    /* Fills in the array with values to the input */
    for(i=0;i<rows;i++)
    {
    for(j=0;j<columns;j++)
    {
    fscanf(input,"%lf", &move);
    *(array[i]+j)=move;
    }
    }

    /* Copies array elements into output file */
    for(i=0;i=rows;i++)
    {
    for(j=0;j<columns;j++)
    fprintf(output,"%lf\t",*array[i]+j);
    fprintf(output,"\n");
    }
    return EXIT_SUCCESS;
    }

  6. #6
    Unregistered
    Guest
    Nevermind, I believe I have figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-30-2005, 12:24 AM
  3. Reading encrypted data from file
    By cctoombs in forum C Programming
    Replies: 2
    Last Post: 02-13-2005, 02:59 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21