[SOLVED] Trying to take name listings out of file and put into array (complete novice)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.
You may have to explain your requirements in more detail. Are you talking about scripting, some specific programming language, or does it matter which tool you use? And do you only want to reformat each line into "last, first", or do you need the values stored in arrays for other purposes as well? Are you even sure that an array is what you need to use?
I need to be able to do some other things once I have the array; sort and search; but I think I can figure that out once I can actually get the array. I just don't know how to go about getting filling the array from the file.
Again, what language? And again, please provide us with some more details on what you want to do. How about showing us an actual example of the input, and how you want the data to be stored/manipulated?
From what you've said so far, it sounds to me like you might need some kind of multi-dimensional array to keep track of each separate entry as well as the individual data fields for them. bash can't do that directly, but it might be possible with linked associative arrays. Other languages might be more suited to the task.
Sorry about that. The language is just C and the input file would look something like,
names.txt
John Smith
Jane Doe
Jane Smith
John Doe
Jake Doe
Jake Smith
While the array has to look like,
Smith, John
Doe, Jane
Smith, Jane
etc ...
I think what I would have to do is individually read the characters from the file and copy them into the row of the array until I reach a new line and then go to the next index and do so again until I am at the end of the file. I have no idea how to do this, though. Once I have that array I think I could just use strcmp and strcpy to search and sort it, which is all I need to do.
Just spent the last half hour typing this up and I know there are a lot of problems.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define LEN 50
#define NAMES 10
//function declarions
bool menu(char list[][]);
void printArray(char list[][]);
void sortArray(char list[][]);
void searchArray(char list[][]);
int main(int argc, char* argv[])
{
char list[NAMES][LEN];
FILE* fpIn;
int i;
int cInput;
if (argc != 2)
printf("\nWrong amount of command line arguments\n");
else if (!(fpIn = fopen(argv[1], "r")))
printf("\nFile can't open");
else
{
for (i = 0; i < NAMES; i++)
{
while((cInput = fgetc(fpIn)) != 10)
*list[i] = cInput;
}
menu(list);
}
fclose(fpIn);
return 0;
}
bool menu(char list[][LEN])
{
int userInput;
bool endMenu = true;
printf("\nPlease enter one of the following options: ");
printf("\n 1: Sort in ascending ASCII order by last name");
printf("\n 2: Search for a particular name");
printf("\n 3: Print the collection of names");
printf("\n 4: Quit\n\n");
scanf("%d", userInput);
if (userInput == 1)
{
sortArray(list);
printArray(list);
menu(list);
}
else if (userInput == 2)
{
searchArray(list);
menu(list);
}
else if (userInput == 3)
{
printArray(list);
menu(list);
}
else if (userInput == 4)
endMenu = false;
else
menu(list);
return endMenu;
}
void printArray(char list[][LEN])
{
int i;
for (i = 0; i < NAMES; i++)
printf("\n%s", list[i]);
}
void searchArray(char list[][LEN])
{
int i;
char name[LEN];
int result = -1;
printf("\nPlease enter a name to search for: ");
scanf("%s", name);
for (i = 0; i < NAMES && result == -1; i++)
if(strcmp(list[i], name) == 0)
result = i;
if(result == -1)
printf("\nUnable to find name");
else
printf("\nName found at position %d in array", i);
}
void sortArray(char list[][LEN])
{
char temp[LEN];
int i;
for (i = 0; i < NAMES; i++)
{
if(strcmp(list[i], list[i+1]) > 0)
{
strcpy(temp, list[i]);
strcpy(list[i], list[i+1]);
strcpy(list[i+1], temp);
}
}
}
1. Every time I enter an integer when prompted I get a segmentation fault
2. When I run it without a valid file name in the command prompt I get a segmentation fault
3. I am sure I didn't fill the array correctly, if I filled it at all
Last edited by abaddon4180; 12-01-2011 at 07:57 PM.
Okay. I think I have most of the coding done, besides some formatting of the names, but every time I get to the menu and enter an integer I still get a segmentation fault. I know this is probably something stupid that I am doing, I just don't know what.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define LEN 50
#define NAMES 10
//function declarions
bool menu(char list[][]);
void printArray(char list[][]);
void sortArray(char list[][]);
void searchArray(char list[][]);
int main(int argc, char* argv[])
{
char list[NAMES][LEN];
char name[50];
FILE* fpIn;
int i;
int cInput;
if (argc != 2)
printf("\nWrong amount of command line arguments\n\n");
else if (!(fpIn = fopen(argv[1], "r")))
printf("\nFile can't open\n\n");
else
{
while (!feof(fpIn))
{
fscanf(fpIn, "%s", name);
strcpy(list[i], name);
i++;
}
fclose(fpIn);
menu(list);
}
return 0;
}
bool menu(char list[][LEN])
{
int userInput;
bool endMenu = true;
printf("\nPlease enter one of the following options: ");
printf("\n 1: Sort in ascending ASCII order by last name");
printf("\n 2: Search for a particular name");
printf("\n 3: Print the collection of names");
printf("\n 4: Quit\n\n");
scanf("%d", userInput);
switch (userInput)
{
case 1: sortArray(list);
printArray(list);
break;
case 2: searchArray(list);
menu(list);
break;
case 3: printArray(list);
menu(list);
break;
case 4: endMenu = false;
default: menu(list);
}
return endMenu;
}
void printArray(char list[][LEN])
{
int i;
for (i = 0; i < NAMES; i++)
printf("\n%s", list[i]);
}
void searchArray(char list[][LEN])
{
int i;
char name[LEN];
int result = -1;
printf("\nPlease enter a name to search for: ");
scanf("%s", name);
for (i = 0; i < NAMES && result == -1; i++)
if(strcmp(list[i], name) == 0)
result = i;
if(result == -1)
printf("\nUnable to find name");
else
printf("\nName found at position %d in array", i);
}
void sortArray(char list[][LEN])
{
char temp[LEN];
int i;
for (i = 0; i < NAMES; i++)
{
if(strcmp(list[i], list[i+1]) > 0)
{
strcpy(temp, list[i]);
strcpy(list[i], list[i+1]);
strcpy(list[i+1], temp);
}
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.