14 Replies - 1634 Views - Last Post: 16 March 2011 - 08:08 PM Rate Topic: -----

#1 Okysho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 96
  • Joined: 09-February 11

Read a file from a specific line

Posted 16 March 2011 - 02:56 PM

Hi again!

Not that much help needed this time thankfully, but in case anyone might need to do something similar I'll outline everything.

The object of my assignment is to read data from a file, capitalize the first letter of all the names within the file and rewrite the file. Simple enough right?

Just one small problem. The file we have to use has a title. It looks like this:

ID FIRSTNAME LASTNAME
10 john doe
20 mary jane
30 jim smith

This ID FIRSTNAME LASTNAME bit here messes up my file reading. I need to know how to read a file from whatever line I want. My code is as follows:

#include <stdio.h>
struct employee
{
	char firstname[40];
	char lastname[40];
	int id;
};
typedef struct employee Employee;

Employee e[3];

void PrintEmployeeRecord();
void SaveEmployeeRecord();
//void Capitalize(const Employee e[]);

int main ()
{
int i=0, j =0, r=0, c=0;
Employee e[3];


FILE *file;	
	
file = fopen("employee.dat", "r");// open file

if(file ==NULL){
printf("Cannot create file");//initiate check
return;
}

for (i=0; i<3; i++)
{
fscanf(file,"%d", &e[i].id);
fscanf(file,"%s", e[i].firstname);
fscanf(file,"%s", e[i].lastname);
}


fclose(file); //close file

PrintEmployeeRecord();

//Capitalize(e);

PrintEmployeeRecord();

SaveEmployeeRecord(e);
}
//END MAIN FUNCTION


//purpose: Save employee record to file
//input: array of 3 employee data
//output: file with 3 employee data
void SaveEmployeeRecord(const Employee e[])
{
int i;
FILE *file;	
	
file = fopen("employee.dat", "w"); // open file

if(file ==NULL){
printf("Cannot create file");//initiate check
return;
}

fprintf(file, "ID FIRSTNAME LASTNAME\n");  // print starter data

for (i=0; i<3; i++)
{
fprintf(file, "%d %s %s \n", e[i].id, e[i].firstname, e[i].lastname); //print file data
	}
printf("\n Written to file! \n");

fclose(file);// close file
}

//purpose: Print Employee data
//input: array of 3 employee data
//output: 3 employee data printed to screen
void PrintEmployeeRecord()
{
int i;
for (i=0; i<3; i++);
{
printf("%d %s %s \n", e[i].id, e[i].firstname, e[i].lastname);
}
}



Since I haven't been able to properly read the file, I therefore haven't written my capitalize function (hence why it's commented out) but I suspect that I'll simply be using toupper(); in order to do this. (if this isn't the right way to go, please tell me)

That's all for now

~Oky

Is This A Good Question/Topic? 0
  • +

Replies To: Read a file from a specific line

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5676
  • View blog
  • Posts: 22,537
  • Joined: 23-August 08

Re: Read a file from a specific line

Posted 16 March 2011 - 03:07 PM

It's so very simple: read the line and "discard" it; i.e., don't do anything with the data you read.
Was This Post Helpful? 0
  • +
  • -

#3 Okysho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 96
  • Joined: 09-February 11

Re: Read a file from a specific line

Posted 16 March 2011 - 03:19 PM

Hey again Jack! Thanks for always coming to my rescue. There's just one little hitch with your suggestion. (after thinking it over it really does seem like the most efficient thing to do)

but in my structure the employee ID is an int value (no I can't change it) and there are no ints in the first line.

This actually threw me a logical error when I ran it the first time and I thought the file wasn't reading properly, it output:

ID FIRSTNAME LASTNAME
1606664232 ID FIRSTNAME
32767 LASTNAME 10
0 john doe

It technically it still does this, but I know that it isn't a reading error at the very least.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5676
  • View blog
  • Posts: 22,537
  • Joined: 23-August 08

Re: Read a file from a specific line

Posted 16 March 2011 - 03:21 PM

Just create a char array of BUFSIZ and do a scanf of the first line (before you enter the loop) into that variable.
Was This Post Helpful? 1
  • +
  • -

#5 Okysho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 96
  • Joined: 09-February 11

Re: Read a file from a specific line

Posted 16 March 2011 - 03:33 PM

that makes sense. Thanks Jack! I'll bump this if I run into anything else!
Was This Post Helpful? 0
  • +
  • -

#6 Okysho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 96
  • Joined: 09-February 11

Re: Read a file from a specific line

Posted 16 March 2011 - 03:47 PM

Well I ran into another little problem so I thought I'd bump this one.

If you noticed, my information is stored within a struct. As such when capitalizing I have to reference the first letter of the first and last names in the struct.

I've tried the following:
toupper(e[i].firstname[0]);
toupper(e[i].lasttname[0]);


But it doesn't work. Is there even a way to do this?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: Read a file from a specific line

Posted 16 March 2011 - 04:06 PM

Please show where you tried to convert the first character to upper case.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 Okysho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 96
  • Joined: 09-February 11

Re: Read a file from a specific line

Posted 16 March 2011 - 05:52 PM

Hey Jim! I created a separate function to capitalize the letters. It goes a little somethin' like this
int Capiralize(e)
{
int i;
for(i=0; i<3; i++)
{
toupper(e[i].firstname[0]);
toupper(e[i].lasttname[0]);
}
return (e);
}



Where (e) is the Employee structure. That is the same as in the OP

This post has been edited by Okysho: 16 March 2011 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,399
  • Joined: 19-February 09

Re: Read a file from a specific line

Posted 16 March 2011 - 06:10 PM

The function toupper() returns the uppercase, but does not change the original character value.
Was This Post Helpful? 0
  • +
  • -

#10 Okysho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 96
  • Joined: 09-February 11

Re: Read a file from a specific line

Posted 16 March 2011 - 06:15 PM

Should I then try and derefernce it? What would that even look like? The pointer reference is getting messy...
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,399
  • Joined: 19-February 09

Re: Read a file from a specific line

Posted 16 March 2011 - 06:45 PM

View PostOkysho, on 17 March 2011 - 03:15 AM, said:

Should I then try and derefernce it? What would that even look like? The pointer reference is getting messy...


No, just assign the return value to the original character.

e[i].firstname[0] = toupper(e[i].firstname[0]);


Was This Post Helpful? 1
  • +
  • -

#12 Okysho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 96
  • Joined: 09-February 11

Re: Read a file from a specific line

Posted 16 March 2011 - 06:51 PM

That makes sense... I can't believe I didn't think of that before.

However, upon application, I get a compiling error. It looks like this:

fixcap.c:102: error: subscripted value is neither array nor pointer
fixcap.c:102: error: subscripted value is neither array nor pointer
fixcap.c:103: error: subscripted value is neither array nor pointer
fixcap.c:103: error: subscripted value is neither array nor pointer
Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,399
  • Joined: 19-February 09

Re: Read a file from a specific line

Posted 16 March 2011 - 07:15 PM

Your function definition doesn't look correct - no data type in the parameter list.

int Capiralize(e)
{
  ...


Was This Post Helpful? 0
  • +
  • -

#14 Okysho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 96
  • Joined: 09-February 11

Re: Read a file from a specific line

Posted 16 March 2011 - 07:47 PM

woah! There's another big one I never noticed. I fixed that one up, but there's still the same error that I stated previously. I'm pretty lost as to how to fix this since it's saying I'm not referencing a pointer. While there's no pointer declared, I don't see the error.
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,399
  • Joined: 19-February 09

Re: Read a file from a specific line

Posted 16 March 2011 - 08:08 PM

Post your new code please.

I think your function should be defined like so.

int Capiralize(Employee e[])
{
  ...



This post has been edited by #define: 16 March 2011 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1