我有一个练习,我必须打印文件夹中存在的文档名称列表。
通过该程序,我可以为每个新文档键入详细信息,包括文档的名称。基本上我必须构建2个结构:
在Folder的struct中,有一个嵌套的struct应该包含Document的详细信息(Document struct
)。据我所知,我必须将每个文档的详细信息存储在文件夹中。
我的问题是如何将文档的详细信息存储在文件夹中,以及如何使用PrintFolderDetails
功能进行打印。写下双重astrics是否正确:struct document **docs
;
我已经尝试在复制文档后在option 4
中进行分配,但我收到了消息:
访问违规阅读位置......
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 100
typedef struct document
{
char *docName;
int linesAmountInDoc;
char **docContent;
}Document;
typedef struct folder
{
char folderName[20];
struct document **docs;
}Folder;
void NewFolder();
void MainMenu();
Document *NewDocument(char *);
void EditDocument(Document *);
void PrintDocument(Document *);
Document *CopyDocument(Document *, int numOfDocuments);
void DeleteDocument(Document *);
void ListDocsInFolder();
void PrintFolderDetailes(Folder *fldr, int numOfDocuments);
void main()
{
NewFolder();
MainMenu();
}
//Creates the new Folder
void NewFolder()
{
Document *docs =(Document *)malloc(sizeof(Document));
Folder *folder=(Folder *)malloc(sizeof(Folder));
char *foldName;
int folderSize;
foldName=(char *)malloc(sizeof(char )*20);
printf("Please enter the Folder's name : ");
scanf("%s", foldName);
strcpy(folder->folderName, foldName);
printf("How many documents do you want in your Folder : ");
scanf("%d", &folderSize);
folder->docs=(Document **)malloc(sizeof(Document )*folderSize);
PrintFolderDetailes(folder, folderSize);
printf("A new Folder has been created \n ");
}
void MainMenu()
{
char quit ;
int menuOption, numOfDocuments=0;
Document *doc=(Document *)malloc(sizeof(Document));
Document *copyDoc=(Document *)malloc(sizeof(Document));
Folder *folder=(Folder *)malloc(sizeof(Folder));
//folder->docs= (Document **)malloc(sizeof(Document)*10);
do
{
printf("\nWelcom to Document System \n");
printf("1. Create Docuent \n");
printf("2. Edit Document \n");
printf("3. Print Document \n");
printf("4. Copy Document \n");
printf("5. Delete Document \n");
printf("6. Show List of Documents in Folder \n");
printf("\nChoose one of the Options above : ");
scanf("%d", &menuOption);
switch(menuOption)
{
case 1:
printf("Enter your Document name : ");
doc->docName=(char *)malloc(sizeof(char *)*LENGTH);
scanf("%s", doc->docName);
doc=NewDocument(doc->docName);
break;
case 2:
EditDocument(doc);
break;
case 3:
PrintDocument(doc);
break;
case 4:
numOfDocuments+=1;
copyDoc=CopyDocument(doc, numOfDocuments);
(*folder).docs=(Folder **)malloc(sizeof(Folder **)*numOfDocuments);
strcpy((*folder).docs[numOfDocuments]->docName, copyDoc->docName); //access violation
//strcpy(folder->docs[numOfDocuments].docName,copyDoc->docName); //access violation
break;
case 5:
DeleteDocument(doc);
break;
case 6:
PrintFolderDetailes(folder, numOfDocuments);
break;
default:
scanf("%c", &quit) ;
break;
}
quit=getchar();
//getchar();
}while (quit != 'q');
}
Document* NewDocument(char *docName)
{
Document *newDocument;
newDocument=(Document*)malloc(sizeof(Document));
newDocument->docName= docName;
newDocument->linesAmountInDoc=0;
newDocument->docContent=(char **)malloc(sizeof(char));
return newDocument;
}
void EditDocument(Document *document)
{
int i;
printf("Please enter How many lines do you want to write ? ");
scanf("%d", &(*document).linesAmountInDoc);
printf("You can write %d lines in your Document \n ",
(*document).linesAmountInDoc-1);
//The number of lines in Document
(*document).docContent=(char **)malloc(sizeof(char **)*(*document).linesAmountInDoc);
printf("Please enter the content of the Document \n");
for(i=0;i<document->linesAmountInDoc;i++)
{
(*document).docContent[i]=(char *)malloc(sizeof(char *)*LENGTH);
//the length of each line
//scanf("%s", (*document).docContent[i]); //Typing the sentence without spaces between words
gets((*document).docContent[i]);
}
}
void PrintDocument(Document *document)
{
int i=0;
printf("Document name : ");
printf("%s \n", document->docName);
puts("========================");
for(i=0;i<(*document).linesAmountInDoc;i++)
{
printf("%s\n", (*document).docContent[i]);
}
}
Document *CopyDocument(Document *document, int numOfDocuments)
{
char *documentName=(char *)malloc(sizeof(char)*8);
char *documentNumber=(char *)malloc(sizeof(char)*2);
char *documentNameAndNumber=(char *)malloc(sizeof(strlen(document->docName)+1));
strcpy(documentName, document->docName);
sprintf(documentNumber, "%d" , numOfDocuments) ; //casting the document's serial number to characters
strcpy(documentNameAndNumber, documentName);
strcat(documentNameAndNumber, documentNumber); //concatenate the document's name & number
strcpy(document->docName, documentNameAndNumber); //Good!!!
//For checking the copying results
PrintDocument(document);
printf("The Document has been copied \n");
return document;
}
void DeleteDocument(Document *document)
{
free((*document).docName);
free((*document).docContent);
free(document);
document=NULL;
printf("The tow Documents has been deleted");
}
void PrintFolderDetailes(Folder *folder, int numOfDocuments)
{
int i=0;
printf("Folder name : ");
printf("%s \n", folder->folderName);
puts("========================");
if(numOfDocuments==0)
{
printf("The Folder %s is empty \n", folder->folderName);
return;
}
else
{
for(i=0;i<numOfDocuments;i++)
{
//strcpy((*folder).docs[i].docName,doc->docName);
printf("%s\n", (*folder).docs[i]->docName);
}
}
}
答案 0 :(得分:0)
在案例4中,您正在分配numOfDocuments-1
大小数组,这意味着数组的范围是docs[numOfDocuments]
到docs[numOfDocuments-1]
- 所以在下一行中您不应该访问(*folder).docs=(Folder **)malloc(sizeof(Folder **)*numOfDocuments)
,你可能打算使用folder->docs= malloc(sizeof(Folder *)*numOfDocuments)
无论如何,您在代码中还有其他一些问题。例如,在以下行中:
p->
应该是
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String localeString = ims.getLocale();
Locale locale = new Locale(localeString);
String currentLanguage = locale.getDisplayLanguage();
if(Keyboard input method.equals(currentLanguage)
{
//do something
}
var config = {
paths: {
myFiles: 'folder/files'
},
task: {
sync: {
files: [
config.paths.myFiles + '/*.jade'
]
},
[...]
};
代替(* p) - 它只是语义,但这是常用方法。