I'm fairly new to programming and this is my first question here so hopefully I don't break any (too many) rules! I usually like to figure things out on my own but I'm stumped so here goes:
I have an assignment for my computer science II course that's asking me to create/write my name to a file (let's say John Doe) and then output the contents of that file. This part I've been able to accomplish.
Now the 2nd half of the assignment is telling me to write and use a function that converts my name into Last Name, First name format and write it to a separate file. It doesn't specifically tell me to output the contents of said file but I want to anyway.
I'm not exactly sure how to approach converting the name. Should I create 2 different strings for my first and last name (I thought about doing this but it didn't seem particularly efficient)? It also doesn't seem like a situation that should require utilizing a struct, but I could be (and probably am) completely off on that. Here's what I have so far:
#include <stdio.h>
#define SIZE 20
int main(void)
{
FILE *readPtr;//readfile.txt file pointer
FILE *writePtr;//writefile.txt file pointer
char nameFull[SIZE] = "John Doe";
readPtr = fopen("readfile.txt", "w");//creates readfile.txt file for writing
fprintf(readPtr, "%s", nameFull);//writes contents of string "nameFull" to file
fclose(readPtr);//close file
if((readPtr = fopen("readfile.txt", "r")) == NULL){//returns error message if file can't be opened
printf("Error opening file!\n");
}
else{
while(!feof(readPtr)){//end of file check for readPtr file
fscanf(readPtr, "%[^\n]s", nameFull);
printf("\n%s\n\n", nameFull);
}
fclose(readPtr);
}
}
Any answers or suggestions are greatly appreciated. Even just a nudge in the right direction would be awesome. Also, any suggestions/criticism about my post are welcome too, so that I may improve them going forward. Thanks for your time!