C File manipulation

Newbie to C needs advice

Page 1 of 1

5 Replies - 3244 Views - Last Post: 13 January 2006 - 03:29 PM Rate Topic: -----

#1 Empty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-August 05

C File manipulation

Posted 12 January 2006 - 03:01 PM

Hi

Im trying to read in from a file, then do a few calculations from it, was wondering if anyone would just flick over what i've got so far?


Main ()

Int average


FILE *stream, *fopen();

stream = fopen(``data.txt'',``r'');

/*Validation

if ( (stream = fopen( ``myfile.dat'',
``r'')) == NULL)
{ pringf(``can’t open file\n ``data.txt ``);
Exit(1);
)
At the moment all I've got is declared average and an integer, read from the file data.txt and i think i've validated it so if it doesn't open proper it should print a bad file etc..


Morgan.

[edit]
Being dumb trying to simplify it down a bit here, have only just started C.

#include <stdio.h>

int main()
{

/* Defined variables */
int total, average, highest
int data;

/* open data */
FILE *fp;
fp = fopen ("data.txt", "r");

Not entirely sure how to do an average if given a file full of numbers so that should be fun to work out :). Anyhelp appreciated

This post has been edited by Empty: 12 January 2006 - 03:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C File manipulation

#2 Empty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-August 05

Re: C File manipulation

Posted 12 January 2006 - 03:53 PM

OMG Im going crazy crazy with this crap

#include <stdio.h>

void main(void)

{

/* Defined variables */
int total, average, highest = 0;
int count, data;

/* open data */
FILE *fp;
fp = fopen ("test2data.txt", "r");
int total = 0, average = 0, highest = 0;
int count = 0, data;
while (fscanf(fp, "%d", &data) > 0)
{
// printf("%d\n", data);
total += data;
++count;
if (data > highest)
highest = data;
}

average = total / count;

printf("%d\n", total);
printf("%d\n", average);
printf("%d\n", highest);
printf("%d\n", count);

}

Finished artical i think :( well feel better putting it out there


Morgan.
Was This Post Helpful? 0
  • +
  • -

#3 jaredolympia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 15-October 05

Re: C File manipulation

Posted 13 January 2006 - 12:42 AM

Have you compiled this code? There are several errors, including declaring several variables twice.

This post has been edited by jaredolympia: 13 January 2006 - 12:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 Empty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-August 05

Re: C File manipulation

Posted 13 January 2006 - 01:38 PM

yeah it failed ot compile spent about 2hours trying to sort it out but got nowhere so ho hum




morgan .
Was This Post Helpful? 0
  • +
  • -

#5 jaredolympia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 15-October 05

Re: C File manipulation

Posted 13 January 2006 - 03:10 PM

You may want to get a good book. Sams Teach Yourself C in 21 Days proved to be an excellent programming book for me. It provides quizes to reinforce your knowledge of the C language. You may also want to try a tutorial, although, in my opinion, there not as friendly as a good book.

Sams Teach Yourself C in 21 Days
Was This Post Helpful? 0
  • +
  • -

#6 jaredolympia  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 15-October 05

Re: C File manipulation

Posted 13 January 2006 - 03:29 PM

I'm pretty sure you heard this about a thousand times, but C++ is a superset of C. In other words, a lot of the syntax of C carries over to C++. C is a little more beginer friendly, however. Since you OS preference is Windows, you may be interested in C# which is also OOP. This language takes a lot of the good stuff from C, C++, Java while focusing on simplicity and providing a great IDE (Integretated Development Environment).

After you have downloaded the compiler, you may be interested in some beginning video tutorials. Of course, if you really want to learn the language, I'd get a good book.

Visual C# 2005 Express Edition

This is more or less a trial version, so if you like it, you probably want to buy the retail version.

Absolute Beginner’s Video Series

Microsoft Visual C# .NET Step by Step--Version 2003

Visual Studio also has a 2005 version which you probably want to look out for.

This post has been edited by jaredolympia: 13 January 2006 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1