Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
12-01-2011, 06:47 PM
|
#1
|
LQ Newbie
Registered: Dec 2011
Posts: 7
Rep: 
|
Trying to take name listings out of file and put into array (complete novice)
Like the title says I am trying to read in a file from the command line and then take the data from that, in the form of,
First Last
First Last
First Last
etc ...
And put it into an array of strings formatted as such,
Last, First
Last, First
Last, First
But I really have no idea where to start. Any help for someone just getting into programming would be appreciated.
|
|
|
12-01-2011, 07:32 PM
|
#3
|
LQ Newbie
Registered: Dec 2011
Posts: 7
Original Poster
Rep: 
|
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.
|
|
|
12-01-2011, 07:42 PM
|
#4
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
|
|
12-01-2011, 07:50 PM
|
#5
|
LQ Newbie
Registered: Dec 2011
Posts: 7
Original Poster
Rep: 
|
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.
|
|
|
12-01-2011, 07:58 PM
|
#6
|
LQ Newbie
Registered: Dec 2011
Posts: 7
Original Poster
Rep: 
|
I think this might work but I am not sure, especially when it comes to pointers and such which really confuse me
Code:
int main(int argc, char* argv[])
{
char list[NAMES][LEN];
if (argc != 2)
{
printf("\nWrong amount of command line arguments\n");
return 0;
}
else
getArray(&argv[1], list);
}
void getArray(char* argv[], char list[][LEN])
{
int i;
FILE* fpIn;
int cInput;
fpIn = fopen(argv[1], "r");
for (i = 0; i < NAMES; i++)
{
while((cInput = fgetc(fpIn)) != 10)
* list[i] = putchar(cInput);
}
fclose(fpIn);
}
|
|
|
12-01-2011, 08:32 PM
|
#7
|
LQ Newbie
Registered: Dec 2011
Posts: 7
Original Poster
Rep: 
|
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 08:57 PM.
|
|
|
12-02-2011, 05:45 AM
|
#8
|
LQ Newbie
Registered: Dec 2011
Posts: 7
Original Poster
Rep: 
|
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);
}
}
}
|
|
|
12-02-2011, 05:58 AM
|
#9
|
LQ Newbie
Registered: Dec 2011
Posts: 7
Original Poster
Rep: 
|
Never mind. Just realized I was missing the '&' in the scanf in the menu function. Like I said, I am new to programming.
|
|
|
All times are GMT -5. The time now is 01:05 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|